Несколько удостоверений в ASP.NET Core 2.0
Я перевожу приложение ASP.NET Core 1.0 в ASP.NET Core 2.0.
В моем стартапе я настраиваю две личности:
services.AddIdentity<IdentityUser, IdentityRole>(configureIdentity)
.AddDefaultTokenProviders()
.AddUserStore<IdentityUserStore<IdentityUser>>()
.AddRoleStore<IdentityRoleStore<IdentityRole>>();
services.AddIdentity<Customer, CustomerRole>(configureIdentity)
.AddDefaultTokenProviders()
.AddErrorDescriber<CustomerIdentityErrorDescriber>()
.AddUserStore<CustomerStore<Customer>>()
.AddRoleStore<CustomerRoleStore<CustomerRole>>();
Это работало нормально в ASP.NET Core 1.0, но завершается с ошибкой: System.InvalidOperationException: "Схема уже существует: Identity.Application" в ASP.NET Core 2.0.
В ASP.NET Core 2.0, если я удалю один из вызовов AddIdentity
ошибка уходит. Как мне перенести мой код, чтобы я мог использовать два разных типа идентификации пользователя и роль в моем приложении? Или я просто сделал фундаментальную ошибку в понимании того, как все работает, когда я написал это в ASP.NET Core 1.0?
2 ответа
После просмотра исходного кода ASP.NET Core на github можно добавить второе удостоверение с помощью этого метода расширения:
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System;
using System.Collections.Generic;
using System.Text;
namespace Whatever
{
public static class IdentityExtensions
{
public static IdentityBuilder AddSecondIdentity<TUser, TRole>(
this IServiceCollection services)
where TUser : class
where TRole : class
{
services.TryAddScoped<IUserValidator<TUser>, UserValidator<TUser>>();
services.TryAddScoped<IPasswordValidator<TUser>, PasswordValidator<TUser>>();
services.TryAddScoped<IPasswordHasher<TUser>, PasswordHasher<TUser>>();
services.TryAddScoped<IRoleValidator<TRole>, RoleValidator<TRole>>();
services.TryAddScoped<ISecurityStampValidator, SecurityStampValidator<TUser>>();
services.TryAddScoped<IUserClaimsPrincipalFactory<TUser>, UserClaimsPrincipalFactory<TUser, TRole>>();
services.TryAddScoped<UserManager<TUser>, AspNetUserManager<TUser>>();
services.TryAddScoped<SignInManager<TUser>, SignInManager<TUser>>();
services.TryAddScoped<RoleManager<TRole>, AspNetRoleManager<TRole>>();
return new IdentityBuilder(typeof(TUser), typeof(TRole), services);
}
}
}
Asp.net Core 2.2 предоставляет встроенный метод для этой цели.
AddIdentityCore<TUser>
Как это использовать:
services.AddIdentity<IdentityUser, IdentityRole>(configureIdentity)
.AddDefaultTokenProviders()
.AddUserStore<IdentityUserStore<IdentityUser>>()
.AddRoleStore<IdentityRoleStore<IdentityRole>>();
services.AddIdentityCore<Customer>(configureIdentity)
.AddDefaultTokenProviders()
.AddErrorDescriber<CustomerIdentityErrorDescriber>()
.AddUserStore<CustomerStore<Customer>>()
.AddRoleStore<CustomerRoleStore<CustomerRole>>();
services.AddScoped<RoleManager<Customer>>();
Фактически, прочитайте реализацию этого метода из ядра gp-gub asp.net 2.2.
/// <summary>
/// Adds and configures the identity system for the specified User type. Role services are not added by default
/// but can be added with <see cref="IdentityBuilder.AddRoles{TRole}"/>.
/// </summary>
/// <typeparam name="TUser">The type representing a User in the system.</typeparam>
/// <param name="services">The services available in the application.</param>
/// <param name="setupAction">An action to configure the <see cref="IdentityOptions"/>.</param>
/// <returns>An <see cref="IdentityBuilder"/> for creating and configuring the identity system.</returns>
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>();
services.TryAddScoped<IUserConfirmation<TUser>, DefaultUserConfirmation<TUser>>();
// 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>>();
if (setupAction != null)
{
services.Configure(setupAction);
}
return new IdentityBuilder(typeof(TUser), services);
}
Большое спасибо за ваш ответ, Кит. Это сэкономило мне много времени! Одно небольшое улучшение: мне пришлось настроить некоторые параметры (IdentityOptions) в моем случае. Как например: Правила Сложности пароля.
Поэтому я включил регистрацию Action actionAction. (Это так же, как Microsoft делает это в AddIdentity внутри IdentityServiceCollectionExtension)
public static class IdentityExtensions
{
public static IdentityBuilder AddSecondIdentity<TUser, TRole>(
this IServiceCollection services, Action<IdentityOptions> setupAction)
where TUser : class
where TRole : class
{
services.TryAddScoped<IUserValidator<TUser>, UserValidator<TUser>>();
services.TryAddScoped<IPasswordValidator<TUser>, PasswordValidator<TUser>>();
services.TryAddScoped<IPasswordHasher<TUser>, PasswordHasher<TUser>>();
services.TryAddScoped<IRoleValidator<TRole>, RoleValidator<TRole>>();
services.TryAddScoped<ISecurityStampValidator, SecurityStampValidator<TUser>>();
services.TryAddScoped<IUserClaimsPrincipalFactory<TUser>, UserClaimsPrincipalFactory<TUser, TRole>>();
services.TryAddScoped<UserManager<TUser>, AspNetUserManager<TUser>>();
services.TryAddScoped<SignInManager<TUser>, SignInManager<TUser>>();
services.TryAddScoped<RoleManager<TRole>, AspNetRoleManager<TRole>>();
if (setupAction != null)
services.Configure(setupAction);
return new IdentityBuilder(typeof(TUser), typeof(TRole), services);
}
}