ASP.NET Core 2.0中的多个身份 - c#

我正在将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 Core 2.0中,“方案已存在:Identity.Application”。

在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);
        }
    }
}

如何在ASP.NET Core Web应用程序中增加JSON反序列化MaxDepth限制 - c#

我们正在将ASP.NET Core 2.1与.NET Framework 4.6.2结合使用。我们有一个客户需要向我们的Web应用程序发送一个很大程度上嵌套的json结构。当他们进行此调用时,我们将输出以下日志并返回错误: 读取器的MaxDepth超过了32。路径“ super.long.path.to property”,第1行,位置42111。”我浏览了…

从Axios请求以ASP.NET Core API返回下载文件 - c#

大家好我正在尝试从Axios Request从ASP.NET Core Web API下载文件。这是我的示例API方法。 (基于此stackoverflow question的代码)[HttpPost("download")] public async Task<IActionResult> DownloadFile(){ .…

ASP.net C#崩溃一行 - c#

我有一个母版页,在on load事件中包含以下几行: string menuIDdata = Page.Request.QueryString["mid"]; menuID = 0; // Get the menu ID if (!int.TryParse(menuIDdata, out menuID)) { menuID = 0; } …

将asp.net.core 2.0应用发布到IIS时JavaScript无法运行 - javascript

我是这里的新手。我有一个VS2017 asp.net C#Web应用程序。我目前不在使用Angular。这是一个简单的网站。我隐藏了div,当用户单击菜单项时,相关的div出现,而我隐藏了其余的div。我使用JavaScript完成此任务。在开发和IIS Express中,它可以工作。当我发布到IIS时,它不起作用。静态页面显示“确定”,但菜单按钮不执行任何…

为什么ASP Net Core 2.2不释放内存? - c#

我正在使用以下项目创建的默认项目来测试ASP Net Core 2.2:Visual Studio>文件>新建>项目> ASP NET Core Web应用程序>下一步>创建。在界面上按IIS Express按钮,然后自动转到https://localhost:44356/api/values编辑:重点是,我正在测试ASP…