码迷,mamicode.com
首页 > Web开发 > 详细

ASP.NET Core 实现用户登录验证的最低配置

时间:2017-10-06 18:36:50      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:ica   代码   on()   nbsp   ati   code   await   tar   type   

背景是在一个项目中增加临时登录功能,只需验证用户是否登录即可,所需的最低配置与实现代码如下。

在 Startup 的 ConfigureServices() 方法中添加 Authentication 的配置:

services.AddAuthentication(options =>
{
    options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
}).AddCookie();

在 Startup 的 Configure() 方法中将 Authentication 添加到请求管线:

app.UseAuthentication();

在登录程序中验证通过用户名/密码后,通过下面的代码生成登录 Cookie 并发送给客户端:

var claimsIdentity = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, model.Email) }, "Basic");
var claimsPrincipal = new ClaimsPrincipal(claimsIdentity);
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
    claimsPrincipal);

ASP.NET Core 实现用户登录验证的最低配置

标签:ica   代码   on()   nbsp   ati   code   await   tar   type   

原文地址:http://www.cnblogs.com/dudu/p/7631927.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!