ASP.NET Core 2.0 中的多重身份 [英] Multiple Identities in ASP.NET Core 2.0

查看:28
本文介绍了ASP.NET Core 2.0 中的多重身份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 ASP.NET Core 1.0 应用程序迁移到 ASP.NET Core 2.0.

I am migrating an ASP.NET Core 1.0 application to ASP.NET Core 2.0.

在我的启动中,我配置了两个身份:

In my startup I am configuring two identities:

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: 'Scheme already exists: Identity.Application' in ASP.NET Core 2.0.

This worked fine in ASP.NET Core 1.0 but fails with the error: System.InvalidOperationException: 'Scheme already exists: Identity.Application' in ASP.NET Core 2.0.

在 ASP.NET Core 2.0 中,如果我删除对 AddIdentity 的调用之一,错误就会消失.如何迁移我的代码,以便我可以在我的应用程序中使用两种不同类型的身份用户和角色?或者,当我在 ASP.NET Core 1.0 中编写此代码时,我是否只是在理解事情如何恢复方面犯了一个基本错误?

In ASP.NET Core 2.0, if I remove one of the calls to AddIdentity the error goes away. How do I migrate my code so that I can use two different types of identity user and role in my application? Or did I just make a fundamental error in understanding how things work back when I wrote this in ASP.NET Core 1.0?

推荐答案

在 github 上查看 ASP.NET Core 源代码后,可以使用此扩展方法添加第二个身份:

After looking through the ASP.NET Core source code on github, a second identity could be added using this extension method:

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.0 中的多重身份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆