C# 商店不实施IUserRoleStore<;TUser>;ASP.NET核心标识

C# 商店不实施IUserRoleStore<;TUser>;ASP.NET核心标识,c#,asp.net-core,.net-core,asp.net-identity,asp.net-core-2.1,C#,Asp.net Core,.net Core,Asp.net Identity,Asp.net Core 2.1,我正在使用ASP.NET Core 2.1标识。我已重写IdentityUser,因为我需要在用户上添加一些附加属性 在Startup.cs中 services.AddDefaultIdentity<PortalUser>().AddEntityFrameworkStores<ApplicationDbContext>(); 一切都很好。我可以通过添加用户 _userManager.CreateAsync(user) 然而,当我调用AddToRolesAsync向用户

我正在使用ASP.NET Core 2.1标识。我已重写IdentityUser,因为我需要在用户上添加一些附加属性

在Startup.cs中

services.AddDefaultIdentity<PortalUser>().AddEntityFrameworkStores<ApplicationDbContext>();
一切都很好。我可以通过添加用户

_userManager.CreateAsync(user)
然而,当我调用AddToRolesAsync向用户添加角色时,我得到了一个异常。你知道为什么吗

_userManager.AddToRolesAsync(user, new List<string> { roleName });

{System.NotSupportedException: Store does not implement IUserRoleStore<TUser>.
   at Microsoft.AspNetCore.Identity.UserManager`1.GetUserRoleStore()
   at Microsoft.AspNetCore.Identity.UserManager`1.AddToRolesAsync(TUser user, IEnumerable`1 roles)}
\u userManager.AddToRolesAsync(用户,新列表{roleName});
{System.NotSupportedException:存储区未实现IUserRoleStore。
位于Microsoft.AspNetCore.Identity.UserManager`1.GetUserRoleStore()
位于Microsoft.AspNetCore.Identity.UserManager`1.AddToRolesAsync(TUser用户,IEnumerable`1角色)}

在Startup.cs中,我缺少AddRoles,所以

services.AddDefaultIdentity<PortalUser>()
    .AddEntityFrameworkStores<ApplicationDbContext>();
services.AddDefaultIdentity()
.AddEntityFrameworkStores();
应该是

services.AddDefaultIdentity<PortalUser>()
    .AddRoles<IdentityRole>()
    .AddEntityFrameworkStores<ApplicationDbContext>();
services.AddDefaultIdentity()
.AddRoles()
.AddEntityFrameworkStores();

注意:顺序至关重要
AddRoles
必须位于
AddEntityFrameworkStores

我知道作者已经解决了他的问题,但我将为完成上述答案中所有步骤但仍有此错误的其他人添加此项


您必须删除区域/Identity/IdentityHostingStartup.cs中自动生成的IdentityHostingStartup.Configure方法

,因为在asp.net Core 2.2中没有关于解决方案的任何答案,我想与大家分享我在asp.net Core 2.2中遇到的相同错误

首先,这里是asp.net core 2.1中相同错误的另一个解决方案

多亏了作者的想法,我在遵循asp.net core 2.2中的官方指导时遇到了这个问题(url在这里:)。当我完成他说的步骤并尝试运行该项目时,它抛出一个异常“Store未实现IUserRoleStore”

问题是:实际上,这是asp.net core 2.1的示例(我强烈怀疑微软为什么会向用户提供没有任何示例代码的文档,这可能是没有意义的)

您会发现,在区域/Identity/Data/IdentityHostingStartup.cs IdentityHostingStartup::Configure method中,您有以下代码:

services.AddDefaultIdentity<IdentityUser>().AddEntityFrameworkStores<ApplicationDbContext>();

services.AddDefaultIdentity().AddEntityFrameworkStores,祝你好运:)

我已经覆盖了IdentityUser你这是什么意思?你需要向我们展示你的代码,这个问题目前还不能回答。这很公平。我已经编辑了我的问题。
ApplicationDbContext
继承自什么?identitydbcontext即使您自己回答了问题,您也应该接受这个答案。
services.AddDefaultIdentity<PortalUser>()
    .AddRoles<IdentityRole>()
    .AddEntityFrameworkStores<ApplicationDbContext>();
services.AddDefaultIdentity<IdentityUser>().AddEntityFrameworkStores<ApplicationDbContext>();
services.AddDefaultIdentity<IdentityUser>().AddRoles<IdentityRole>().AddEntityFrameworkStores<ApplicationDbContext>();
services.AddDefaultIdentity<IdentityUser>().AddEntityFrameworkStores<ApplicationDbContext>();
services.AddDefaultIdentity<IdentityUser>().AddRoles<IdentityRole>().AddEntityFrameworkStores<ApplicationDbContext>();