Csharp/C#教程:Store未实现IUserRoleStore ASP.NET Core 2.1 Identity分享


Store未实现IUserRoleStore ASP.NET Core 2.1 Identity

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

在Startup.cs中

services.AddDefaultIdentity().AddEntityFrameworkStores(); 

ApplicationDbContext.cs

 public partial class ApplicationDbContext : IdentityDbContext { public ApplicationDbContext(DbContextOptions options) : base(options) { } } 

PortalUser类

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

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

 _userManager.CreateAsync(user) 

但是,当我调用AddToRolesAsync向用户添加角色时,我遇到了exception。 有什么想法吗?

 _userManager.AddToRolesAsync(user, new List { roleName }); {System.NotSupportedException: Store does not implement IUserRoleStore. at Microsoft.AspNetCore.Identity.UserManager`1.GetUserRoleStore() at Microsoft.AspNetCore.Identity.UserManager`1.AddToRolesAsync(TUser user, IEnumerable`1 roles)} 

在Startup.cs中,我错过了AddRoles

 services.AddDefaultIdentity() .AddEntityFrameworkStores(); 

应该

 services.AddDefaultIdentity() .AddRoles() .AddEntityFrameworkStores(); 

注意:订单很重要。 AddRoles必须在AddEntityFrameworkStores之前

上述就是C#学习教程:Store未实现IUserRoleStore ASP.NET Core 2.1 Identity分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/985551.html

(0)
上一篇 2021年12月21日
下一篇 2021年12月21日

精彩推荐