Store未实现IUserRoleStore <TUser> ASP.NET Core 2.1标识

Joh*_*n81 9 c# asp.net-identity .net-core asp.net-core asp.net-core-2.1

我正在使用ASP.NET Core 2.1 Identity.我已经重写了IdentityUser,因为我需要在用户上添加一些额外的属性.

在Startup.cs中

services.AddDefaultIdentity<PortalUser>().AddEntityFrameworkStores<ApplicationDbContext>();
Run Code Online (Sandbox Code Playgroud)

ApplicationDbContext.cs

public partial class ApplicationDbContext : IdentityDbContext<PortalUser>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
    {

    }
}
Run Code Online (Sandbox Code Playgroud)

PortalUser类

public class PortalUser : IdentityUser
{
    [PersonalData]
    public DateTime? LastLoginDateUtc { get; set; }

    [PersonalData]
    public DateTime? RegistrationDateUtc { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

这一切都很好.我可以通过添加用户.

_userManager.CreateAsync(user)
Run Code Online (Sandbox Code Playgroud)

但是,当我调用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)}
Run Code Online (Sandbox Code Playgroud)

Joh*_*n81 30

在Startup.cs中,我错过了AddRoles

services.AddDefaultIdentity<PortalUser>()
    .AddEntityFrameworkStores<ApplicationDbContext>();
Run Code Online (Sandbox Code Playgroud)

应该

services.AddDefaultIdentity<PortalUser>()
    .AddRoles<IdentityRole>()
    .AddEntityFrameworkStores<ApplicationDbContext>();
Run Code Online (Sandbox Code Playgroud)

注意:订单很重要. AddRoles必须来之前AddEntityFrameworkStores

  • 即使您自己回答了您的问题,您也应该接受这个答案。 (2认同)

Zyu*_*uuu 7

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

首先,这是针对asp.net core 2.1 https://github.com/aspnet/AspNetCore.Docs/issues/8683 中相同错误的另一种解决方案

并且感谢作者的想法,当我按照asp.net core 2.2中的官方指导进行操作时遇到了问题(网址在这里:MicrosoftDocs For asp.net core 2.2)。当我完成他所说的步骤并尝试运行该项目时,它会引发异常“Store 未实现 IUserRoleStore”

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

您会发现,在Areas/Identity/Data/IdentityHostingStartup.cs IdentityHostingStartup::Configure 方法中,您有以下代码:

services.AddDefaultIdentity<IdentityUser>().AddEntityFrameworkStores<ApplicationDbContext>();
Run Code Online (Sandbox Code Playgroud)

这与您应该在/Program.cs ConfigureService 中添加的代码相同,作为步骤:在提到的文档中将角色服务添加到身份

services.AddDefaultIdentity<IdentityUser>().AddRoles<IdentityRole>().AddEntityFrameworkStores<ApplicationDbContext>();
Run Code Online (Sandbox Code Playgroud)

因此,如果您在 asp.net core 2.2 中遇到同样的问题,另一种解决方案是:

  1. 按照asp.net 2.2中的文档
  2. 当您遇到本章:向身份添加角色服务时,请忽略官方文档并执行以下操作:

替换行

services.AddDefaultIdentity<IdentityUser>().AddEntityFrameworkStores<ApplicationDbContext>();
Run Code Online (Sandbox Code Playgroud)

services.AddDefaultIdentity<IdentityUser>().AddRoles<IdentityRole>().AddEntityFrameworkStores<ApplicationDbContext>();
Run Code Online (Sandbox Code Playgroud)

Areas/Identity/Data/IdentityHostingStartup.cs IdentityHostingStartup::Configure方法中,但不要在 program.cs 中添加它(该文件不能在 asp.net core 2.2 中删除)

我使用 Asp.net Identity 的项目稍后将在我的 repos 中更新:UWPHelper,祝你好运:)