一.登录分析

  在使用identity身份验证登录时,在login中调用的方法是:

  var result = await _signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: true);

  跟踪查看源码,源码下载https://github.com/aspnet/AspNetCore/releases 这里有core源码的不同版本,在vs 2017下只能加载2.2及以下的版本。

  下面是登录的大概步骤:

  (1) 检查用户名是否存在(UserManager.cs在Microsoft.AspNetCore.Identity.core源码中)

    var user = await UserManager.FindByNameAsync(userName);

  (2) UserManager类来检查用户名和密码是否存在

    UserManager.CheckPasswordAsync(user, password)

  (3) 登录,isPersistent是指浏览器关闭后登录cookie是否应该保持,如果是true则永久保存cookie,如果为false则使用services.ConfigureApplicationCookie中options.ExpireTimeSpan 来重写。SignInOrTwoFactorAsync(user, isPersistent)方法最终调用SignInAsync进行登录。

       public virtual async Task SignInAsync(TUser user, AuthenticationProperties authenticationProperties, string authenticationMethod = null)
{
var userPrincipal = await CreateUserPrincipalAsync(user);
// Review: should we guard against CreateUserPrincipal returning null?
if (authenticationMethod != null)
{
userPrincipal.Identities.First().AddClaim(new Claim(ClaimTypes.AuthenticationMethod, authenticationMethod));
}
await Context.SignInAsync(IdentityConstants.ApplicationScheme,
userPrincipal,
authenticationProperties ?? new AuthenticationProperties());
}

    AuthenticationProperties:用来存储身份认证会话

    IdentityConstants:是配置Identity系统使用的cookie中间件的所有选项, ApplicationScheme属性是指:该方案运用于Identity应用程序的cookies(默认方案)。如下所示:

          private static readonly string CookiePrefix = "Identity";
public static readonly string ApplicationScheme = CookiePrefix + ".Application"

    登录涉及到三个类ClaimsPrincipal(声明当事人)、ClaimsIdentity(声明标识)、Claim(声明)。

    Claim:是名称值对,比如名称ClaimType:身份证, 值ClaimValue:18位号码。

    ClaimsIdentity:一组Cliams 就构成了一个Identity标识。

    ClaimsPrincipal:当事人可以持有多个ClaimsIdentity标识。

    最后SignInAsync 创建一个加密的 cookie,并将其添加到当前响应。

二.注销

  若要注销(退出登录)当前用户,然后删除其 cookie,需要调用SignOutAsync 。

    await HttpContext.SignOutAsync();    

三. Identity表管理

  3.1可以使用UserManager类和RoleManager类来管理Identity表,可以参考"通过授权创建web应用",下面是声明的新增方法

    //添加用户声明 Microsoft.AspNetCore.Identity.UserManager<TUser>
public virtual Task<IdentityResult> AddClaimAsync(TUser user, Claim claim) //添加角色声明 Microsoft.AspNetCore.Identity.RoleManager<TRole>
public virtual async Task<IdentityResult> AddClaimAsync(TRole role, Claim claim)

  3.2 在UserManager下,会发现很多方法,都是传入ClaimsPrincipal参数,如下所示:

    //获取用户ID
GetUserId(ClaimsPrincipal principal)
//获取用户
Task<TUser> GetUserAsync(ClaimsPrincipal principal)

    可以通过如下来转换成ClaimsPrincipal:

    ClaimsPrincipal principal = HttpContext.Current.User as ClaimsPrincipal;

  3.3 Claim声明类

    声明值Value:对于简单的声明值可以使用字符串存储,更复杂的值类型,建议使用标准的 XML (或json)架构类型,在应用程序端序列化和反序列化。

    声明类型Type:标识值的类型信息。

    其它属性, 如定义颁发声明等,参考官方文档

    

四.不使用identity系统进行身份认证

  如果开发想自定义用户表,角色表等,完全抛弃identity系统,实现参考"使用cookie 而无需ASP.NET Core 标识的身份验证

五. Identity扩展

  (1) 如果想使用不同数据访问方法,不使用默认的EF Core。

  (2) 如果不想使用 SQL Server存储用户信息,想使用其它数据存储。

  (3) 对Identity表想使用不同的结构。

  实现参考"ASP.NET Core标识的自定义的存储提供程序

    

六. Identity配置

  对于 ASP.NET Core Identity设置,例如密码策略、 锁定和 cookie 配置使用默认值等。参考文档 "配置标识"

    

七. 帐户确认和 ASP.NET Core 中的密码恢复

  https://docs.microsoft.com/zh-cn/aspnet/core/security/authentication/accconfirm?view=aspnetcore-2.2&tabs=visual-studio  

    

最新文章

  1. *HDU1285 拓扑排序
  2. css实现三角形箭头
  3. BIND的进阶二:视图,日志,转发,子域的授权
  4. table表格宽度固定,同时td内容过长也不会被撑开
  5. kafka环境搭建及librdkafka测试
  6. spring 以Ant Build方式运行build.xml文件,报warning: &#39;includeantruntime&#39; was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds 的解决办法
  7. HTML 折行 &lt;br/&gt;标签
  8. 如何加载JS
  9. CocoStudio基础教程(2)关联程序逻辑与cocoStudio导出文件
  10. js 正则表达式中的惰性匹配
  11. Java中List转数组,必须带个参数
  12. POJ 1305 Fermat vs. Pythagoras (毕达哥拉斯三元组)
  13. SSO-Javascript模拟IE登录,不让IIS弹出登录窗口
  14. Java多线程(二) 多线程的锁机制
  15. HDU 4617Weapon(两条异面直线的距离)
  16. linux下CPU信息查询
  17. Mac系统中各个文件夹简单介绍(转)
  18. hashchange
  19. iOS网络编程笔记——GCDAsyncSocket使用
  20. git 知识点汇总

热门文章

  1. ASP.NET MVC中的路由IRouteConstraint方法应用实例
  2. centos7虚拟机设置静态ip
  3. git命令行常用几个指令(细节问题)
  4. Linux的安装(虚拟机环境)与基础配置
  5. AUTOSAR的前期开源实现Arctic Core
  6. 关于overflow的问题
  7. 【转】asp.net获取当前页面的url地址
  8. javascript 用函数语句和表达式定义函数的区别详解
  9. 去重是distinct还是group by?
  10. PAT1126:Eulerian Path