kei*_*ith 11 c# asp.net-core asp.net-core-identity
我正在将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>>();
Run Code Online (Sandbox Code Playgroud)
这在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中编写此内容时,我是否只是在理解事情如何工作方面犯了一个根本错误?
kei*_*ith 17
在浏览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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
Asp.net Core 2.2 为此提供了一种内置方法。
AddIdentityCore<TUser>
Run Code Online (Sandbox Code Playgroud)
如何使用它:
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>>();
Run Code Online (Sandbox Code Playgroud)
其实从asp.net core 2.2 github repo中读到这个方法的实现
/// <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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5095 次 |
| 最近记录: |