- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在使用 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)}
最佳答案
在 Startup.cs 中,我缺少 AddRoles
services.AddDefaultIdentity<PortalUser>()
.AddEntityFrameworkStores<ApplicationDbContext>();
应该是
services.AddDefaultIdentity<PortalUser>()
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>();
注意:顺序很重要。 AddRoles
必须在 AddEntityFrameworkStores
关于c# - Store 没有实现 IUserRoleStore<TUser> ASP.NET Core Identity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52522248/
我已经为我的 Web 应用程序实现了自定义用户存储。到目前为止一切正常。现在我想扩展商店类,以支持角色。我实现了以下接口(interface): IUserStore, IUserLockoutSto
我正在使用 ASP.NET Core 2.1 身份。我已经覆盖了 IdentityUser,因为我需要为用户添加一些额外的属性。 在 Startup.cs 中 services.AddDefaultI
我正在尝试在 ASP Net Core 2.1 中使用 Jwt 身份验证和身份 在我的 Startup.cs 中,我有: services.AddAuthentication(JwtBearerDef
我已实现 Identity在 Asp.Net.Core MVC 3因为我们已经有了要使用的数据库表。调用下面的方法时出现错误,它们都发生在 UserManager 中在对方法的同一调用中。查看此方法的
我是一名优秀的程序员,十分优秀!