【发布时间】: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