我正在使用 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)}