En ASP.NET Core, puede agregar varios servicios para la identificación: AddDefaultIdentity
, AddIdentity
y AddIdentityCore
.
¿Cuál es la diferencia entre AddIdentity
y AddIdentityCore
?
AddIdentityCore
agrega los servicios que son necesarios para las acciones de administración de usuarios, como la creación de usuarios, el hash de contraseñas, etc. Aquí está la fuente relevante:
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>>(); ... }
Básicamente, esto se reduce a registrar una instancia de UserManager<TUser>
, pero primero registra todas sus dependencias. Con estos servicios registrados, puede recuperar una instancia de UserManager<TUser>
de DI y crear usuarios, establecer contraseñas, cambiar correos electrónicos, etc.
AddIdentity
registra los mismos servicios que AddIdentityCore
, con algunos extras:
SignInManager
, que efectivamente se encuentra encima del UserManager
como una especie de orquestador. Por ejemplo, PasswordSignInAsync
usa UserManager
para recuperar un usuario, verificar la contraseña (si está configurada) y luego se ocupa de la creación de cookies.AddIdentity
también toma un TRole
y registra los servicios que son necesarios para respaldar los roles. Aquí está la fuente AddIdentity
para completar:
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>>(); ... }