【问题标题】:Store does not implement IUserRoleStore<TUser> ASP.NET Core IdentityStore 未实现 IUserRoleStore<TUser> ASP.NET Core Identity
【发布时间】:2023-03-28 18:28:01
【问题描述】:

我正在使用 ASP.NET Core 2.1 标识。我已经覆盖了 IdentityUser,因为我需要在用户上添加一些额外的属性。

在 Startup.cs 中

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

ApplicationDbContext.cs

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

    }
}

PortalUser 类

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

    [PersonalData]
    public DateTime? RegistrationDateUtc { get; set; }
}

一切正常。我可以通过添加用户。

_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)}

【问题讨论】:

  • 我已经覆盖了 IdentityUser 你这是什么意思?您需要向我们展示您的代码,这个问题目前无法回答。
  • 很公平。我已经编辑了我的问题。
  • ApplicationDbContext 继承自什么?
  • IdentityDbContext

标签: c# asp.net-core .net-core asp.net-identity asp.net-core-2.1


【解决方案1】:

在 Startup.cs 中,我缺少 AddRoles

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

应该是

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

注意:顺序很关键。 AddRoles 必须在 AddEntityFrameworkStores 之前

【讨论】:

  • 即使您自己回答了问题,您也应该接受这个答案。
【解决方案2】:

对于 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 的示例(我强烈怀疑为什么微软会为用户提供一个没有任何示例代码的文档,这可能没有任何意义)

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

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

这与您应该在 /Program.cs ConfigureService 中添加的代码作为步骤:Add Role services to Identity 在提到的文档中:

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

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

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

替换行

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

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

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

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

【讨论】:

    【解决方案3】:

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

    Aspnet github

    您必须删除 Areas/Identity/IdentityHostingStartup.cs

    中自动生成的 IdentityHostingStartup.Configure 方法

    【讨论】:

      猜你喜欢
      • 2020-02-27
      • 1970-01-01
      • 2019-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-02
      • 1970-01-01
      相关资源
      最近更新 更多