AddIdentity 与 AddIdentityCore

2023-12-19

在ASP.NET Core中,可以添加各种服务进行识别:AddDefaultIdentity, AddIdentity and AddIdentityCore.

有什么区别AddIdentity and AddIdentityCore?


AddIdentityCore添加用户管理操作所需的服务,例如创建用户、散列密码等。以下是相关内容source https://github.com/aspnet/AspNetCore/blob/release/3.1/src/Identity/Extensions.Core/src/IdentityServiceCollectionExtensions.cs#L33-L56:

public static IdentityBuilder AddIdentityCore<TUser>(this IServiceCollection services, Action<IdentityOptions> setupAction)
    where TUser : class
{
    // Services identity depends on
    services.AddOptions().AddLogging();

    // Services used by identity
    services.TryAddScoped<IUserValidator<TUser>, UserValidator<TUser>>();
    services.TryAddScoped<IPasswordValidator<TUser>, PasswordValidator<TUser>>();
    services.TryAddScoped<IPasswordHasher<TUser>, PasswordHasher<TUser>>();
    services.TryAddScoped<ILookupNormalizer, UpperInvariantLookupNormalizer>();
    
    // No interface for the error describer so we can add errors without rev'ing the interface
    services.TryAddScoped<IdentityErrorDescriber>();
    services.TryAddScoped<IUserClaimsPrincipalFactory<TUser>, UserClaimsPrincipalFactory<TUser>>();
    services.TryAddScoped<UserManager<TUser>>();

    ...
}

本质上,这可以归结为注册一个实例UserManager<TUser>,但首先注册其所有依赖项。注册这些服务后,您可以检索UserManager<TUser>从 DI 并创建用户、设置密码、更改电子邮件等。

AddIdentity注册相同的服务AddIdentityCore,还有一些额外的内容:

  • 针对应用程序本身、外部登录(例如 Facebook 和 Google)和 2FA 的基于 Cookie 的身份验证方案。
  • The SignInManager,它实际上位于UserManager作为一种协调者。例如,PasswordSignInAsync uses UserManager要检索用户,请验证密码(如果已设置),然后负责 cookie 的创建。
  • AddIdentity本身也需要一个TRole并注册支持角色所需的服务。

这是AddIdentity source https://github.com/aspnet/AspNetCore/blob/release/3.1/src/Identity/Core/src/IdentityServiceCollectionExtensions.cs#L38-L102为了完整性:

public static IdentityBuilder AddIdentity<TUser, TRole>(this IServiceCollection services, Action<IdentityOptions> setupAction)
    where TUser : class
    where TRole : class
{
    // Services used by identity
    services.AddAuthentication(options =>
    {
        options.DefaultAuthenticateScheme = IdentityConstants.ApplicationScheme;
        options.DefaultChallengeScheme = IdentityConstants.ApplicationScheme;
        options.DefaultSignInScheme = IdentityConstants.ExternalScheme;
    })
    .AddCookie(IdentityConstants.ApplicationScheme, o =>
    {
        o.LoginPath = new PathString("/Account/Login");
        o.Events = new CookieAuthenticationEvents
        {
            OnValidatePrincipal = SecurityStampValidator.ValidatePrincipalAsync
        };
    })
    .AddCookie(IdentityConstants.ExternalScheme, o =>
    {
        o.Cookie.Name = IdentityConstants.ExternalScheme;
        o.ExpireTimeSpan = TimeSpan.FromMinutes(5);
    })
    .AddCookie(IdentityConstants.TwoFactorRememberMeScheme, o =>
    {
        o.Cookie.Name = IdentityConstants.TwoFactorRememberMeScheme;
        o.Events = new CookieAuthenticationEvents
        {
            OnValidatePrincipal = SecurityStampValidator.ValidateAsync<ITwoFactorSecurityStampValidator>
        };
    })
    .AddCookie(IdentityConstants.TwoFactorUserIdScheme, o =>
    {
        o.Cookie.Name = IdentityConstants.TwoFactorUserIdScheme;
        o.ExpireTimeSpan = TimeSpan.FromMinutes(5);
    });

    // Hosting doesn't add IHttpContextAccessor by default
    services.AddHttpContextAccessor();
    
    // Identity services
    services.TryAddScoped<IUserValidator<TUser>, UserValidator<TUser>>();
    services.TryAddScoped<IPasswordValidator<TUser>, PasswordValidator<TUser>>();
    services.TryAddScoped<IPasswordHasher<TUser>, PasswordHasher<TUser>>();
    services.TryAddScoped<ILookupNormalizer, UpperInvariantLookupNormalizer>();
    services.TryAddScoped<IRoleValidator<TRole>, RoleValidator<TRole>>();
    // No interface for the error describer so we can add errors without rev'ing the interface
    services.TryAddScoped<IdentityErrorDescriber>();
    services.TryAddScoped<ISecurityStampValidator, SecurityStampValidator<TUser>>();
    services.TryAddScoped<ITwoFactorSecurityStampValidator, TwoFactorSecurityStampValidator<TUser>>();
    services.TryAddScoped<IUserClaimsPrincipalFactory<TUser>, UserClaimsPrincipalFactory<TUser, TRole>>();
    services.TryAddScoped<UserManager<TUser>>();
    services.TryAddScoped<SignInManager<TUser>>();
    services.TryAddScoped<RoleManager<TRole>>();

    ...
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

AddIdentity 与 AddIdentityCore 的相关文章

随机推荐

  • 使 Android Textview 或 EditText 可选择

    我要实现EditText or Textview在我的android项目中可以选择 项目适用于 android 4 0 我添加这个 txtView setTextIsSelectable true 并且 txtView setCustomS
  • 在 Python 中创建自签名 X509 证书

    我跟着this url https web archive org web 20161107073715 http blog richardknop com 2012 08 create a self signed x509 certifi
  • 彩色 Git diff 到 HTML

    我喜欢使用git diff color words要清楚地看到文件中已更改的单词 不过 我想与没有 git 或彩色终端的人分享这个差异 那么有人知道可以转换的工具或技巧吗颜色转义终端输出转换为 HTML wget http www pixe
  • 如何从 ASP.NET 页面获取当前登录的 Windows 帐户?

    我有一个使用 ASP NET 表单身份验证的 ASP NET 3 5 应用程序 我希望在页面中编辑数据时能够获取当前登录到计算机的 Windows 用户名 不是登录到 ASP NET 应用程序 而是登录到 Windows 如果我使用Cont
  • 如何在 Spring Boot 应用程序中使用 Thymeleaf 加载 HashMap 和 ModelandView 对象值?

    对于 Spring boot 应用程序 我无法使用 Thymeleaf 从 HTML 文件加载浏览器中的输入和实例值 下面是控制器 java 文件中的代码片段 RequestMapping value x public String lau
  • 验证 OpenSSL 0.9.8a 中的 RSA 签名

    我正在开发一个基于 OpenSSL 版本 0 9 8a API 的应用程序 我需要使用 RSA 公钥验证 RSA 签名 4096 位 RSA 密钥 pubkey 这是我的代码 const EVP MD md EVP get digestby
  • jQuery addClass() 添加到append() 之后生成的元素

    我正在尝试将一个类添加到新附加的 DIV 中 而不使用以下内容 t y append div class div 这是我正在尝试做的一个更好的例子 var t this this x each function i obj append n
  • 狗年到人年,反之亦然[关闭]

    Closed 这个问题需要细节或清晰度 help closed questions 目前不接受答案 我正在尝试这个示例问题 我必须创建两个隐式转换运算符才能从人类创建 Doggy 类 反之亦然 这些课程需要考虑到人类的一岁是狗的七岁这一事实
  • 将 log4j2 与 slf4j 一起使用:java.lang.StackOverflowError

    所以我尝试过以下this https stackoverflow com questions 25386651 slf4j log4j2 maven setup query 非 Maven 实现 及其要求web site https log
  • MySQL - 在 where 子句中引用聚合列

    这看起来很简单 但我似乎无法在不执行子查询的情况下弄清楚它 这似乎会显着减慢查询速度 需要几乎 10 秒而不是 假设我有一个已发送文档表 我想选择自上次发送以来已更新的文档以及从未发送过的文档 SELECT d document id ma
  • 有人可以更好地解释解码器/编码器吗?

    修改后的问题 好的 所以我正在尝试将其合并到我自己的定制游戏中 我了解了Netty服务器和客户端如何连接的过程 我还了解解码器和编码器在理论上是如何工作的 但这是我仍然想了解的 我的服务器进程 Server boots up gt Clie
  • 如何使哈希键查找不区分大小写?

    显然 哈希键以区分大小写的方式进行比较 perl e hash FOO gt 1 printf s n exists hash foo Yes No No perl e hash FOO gt 1 printf s n exists has
  • 记录应用程序块 - 记录调用者

    使用 Log4Net 进行日志记录时 很容易将调用日志的类放入日志文件中 我过去发现这使得跟踪代码和查看类的流程变得非常容易 在 Log4Net 中 我在转换模式中使用 logger 属性 如下所示
  • Subprocess.Popen 几秒钟后停止(或发生故障)

    我是一个完全的初学者 所以对任何错误表示歉意 这是我在 Python 3 5 中的代码 它在 Raspberry Pi 3 上以 Raspbian 执行 import subprocess radio subprocess Popen mp
  • 重写 Enum._generate_next_value_ 无法按 MRO 的预期工作?

    为什么会覆盖 generate next value 仅在最后继承的枚举中完成才有效 例如 class AutoEnum Enum def generate next value name start count last values r
  • 从表单元素外部的输入标签读取

    我有一个带有输入字段的网页 用于选择日期的日历 问题是我需要从两个不同的表单元素读取输入字段 每个元素都有自己的操作和其他值 calendar do this do that 我不想复制两种表单中的输入字段 因为它看起来很愚蠢 有什么建议么
  • 如何使 SVG 图像动态匹配周围文本的高度 (1em) 和基线?

    是否可以动态调整 SVG 图像的大小 使用 HTML 或 CSS 以匹配周围文本的基线和高度 像这样的东西 在这种情况下 SVG 图像是 Windows 徽标 Image 1 https i stack imgur com Gjyku pn
  • iOS - 实现类似iMessage的滚动效果

    是否可以实现像这样的滚动效果iMessage在 iOS 8 中任何普通UITableView 最好使用 Swift因为它看起来很酷 您正在寻找的是 UIKit Dynamics 我前段时间也对此感兴趣 我参考这个链接来做到这一点 https
  • 更改 cmd 的当前工作目录(从子进程)

    所以我想写一个cd可以使用 cmd 执行的类似程序 并且在退出后 应该更改调用 cmd 进程的工作目录 现在 在这篇文章被标记为重复之前 我知道this https stackoverflow com questions 25944172
  • AddIdentity 与 AddIdentityCore

    在ASP NET Core中 可以添加各种服务进行识别 AddDefaultIdentity AddIdentity and AddIdentityCore 有什么区别AddIdentity and AddIdentityCore AddI