Starup.cs
代码: public void ConfigureServices(IServiceCollection services)
{
//...
services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie();
//...
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
// ...
app.UseAuthentication();
//...
}
AccountController.cs
代码: /// <summary>
/// 登录
/// </summary>
[HttpPost]
public async Task<IActionResult> Login(string account, string password, string returnUrl)
{
//...
var claimIdentity = new ClaimsIdentity("cookie");
claimIdentity.AddClaim(new Claim(ClaimTypes.Name, "1"));
await HttpContext.SignInAsync(
new ClaimsPrincipal(claimIdentity),
new AuthenticationProperties { IsPersistent = true });
//...
}
/// <summary>
/// 退出登录
/// </summary>
public async Task<IActionResult> Logout()
{
await HttpContext.SignOutAsync();
}
原文:https://www.cnblogs.com/cc1027cc/p/11439990.html