我正在将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:'ASP.NET已存在:Identity.Application'在ASP.NET Core 2.0中。
在ASP.NET Core 2.0中,如果我删除对AddIdentity
的一个调用,则错误消失。如何迁移我的代码,以便在我的应用程序中使用两种不同类型的身份用户和角色?或者,当我在ASP.NET Core 1.0中编写此内容时,我是否只是在理解事情如何工作方面犯了一个根本错误?
在浏览github上的ASP.NET Core源代码后,可以使用此扩展方法添加第二个标识:
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);
}
}
}
非常感谢你的答案基思。这为我节省了很多时间!一个小的改进:我必须在我的情况下配置一些选项(IdentityOptions)。例如:密码复杂性规则。
因此,我包括注册Action setupAction。 (这与Microsoft在IdentityServiceCollectionExtension内的AddIdentity中执行此操作的方式相同)
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);
}
}