您的位置:首页 > 编程语言 > ASP

ASP.Net Core Cookie 身份验证

2020-01-11 17:49 507 查看

创建Cookie身份验证

  • 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

  • 点赞
  • 收藏
  • 分享
  • 文章举报
dixian9369 发布了0 篇原创文章 · 获赞 0 · 访问量 58 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: