AddIdentity与AddIdentityCore

Eti*_*and 15 .net c# asp.net-mvc asp.net-core

在ASP.NET的核心,您可以识别添加各种服务:AddDefaultIdentityAddIdentityAddIdentityCore

AddIdentity和之间有什么区别AddIdentityCore

Emr*_*ner 24

AddIdentityCore是向应用程序添加身份验证和授权服务的最简单方法。它提供了一组最少的选项,适用于您希望有一个干净的起点来进一步配置身份验证和授权的高级场景(假设您需要进一步修改以添加更多服务等)。它不会添加某些服务,例如 Cookie 方案、SignInManager 或 RoleManager。

请注意,此方法中的“Core”关键字并不表示它比AddIdentity更新。从字面上看,这仅意味着它具有身份的核心功能。(令人困惑,我知道..)

AddIdentity添加了AddIdentityCore添加的所有内容,以及一些额外的服务,即 Cookie 方案(应用程序、外部和 2FA 方案均已注册)、SignInManager 和 RoleManager。请注意,与AddDefaultIdentity不同, AddIdentity会自动添加角色服务。

AddDefaultIdentity(可以说)是向应用程序添加身份验证和授权服务的最便捷方法,因为它附带了许多适合大多数应用程序的预配置选项,并且它还调用“AddDefaultUI”来添加默认 UI 组件在“身份”区域下。它适用于您希望通过一组合理的默认值和工作 UI 快速启动并运行的场景。

请注意,AddDefaultIdentity不会添加角色服务,因此如果您想使用AddDefaultIdentity并且还希望为您的应用程序提供角色服务(您很可能会这样做),则必须使用它显式调用“AddRoles”,如下所示;

services.AddDefaultIdentity<IdentityUser>(options => { 
    // options are set here
})
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>();
Run Code Online (Sandbox Code Playgroud)

重要提示:AddRoles必须在AddEntityFrameworkStores之前添加,否则你会得到模糊的错误,并且很难找出问题的原因。


Kir*_*kin 19

AddIdentityCore添加用户管理操作所需的服务,例如创建用户,哈希密码等。这是相关的来源

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>>();

    ...
}
Run Code Online (Sandbox Code Playgroud)

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

AddIdentity注册与相同的服务AddIdentityCore,还有一些额外的功能:

  • 应用程序本身,外部登录(例如Facebook和Google)和2FA的基于Cookie的身份验证方案。
  • SignInManager,从而有效地坐在顶部UserManager作为一种配器的。例如,PasswordSignInAsync用于UserManager检索用户,验证密码(如果已设置),然后负责cookie的创建。
  • AddIdentity本身也接受TRole并注册了支持角色所需的服务。

这是完整性的AddIdentity 来源

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>>();

    ...
}
Run Code Online (Sandbox Code Playgroud)

  • @liquidki“AddIdentityCore”的“Core”关键字并不表示它比“AddIdentity”更新。从字面上看,这仅意味着它具有核心功能。 (2认同)