Store does not implement IUserRoleStore<TUser> UserManager<TUser>.GetUserRoleStore() ASP.NET Core MVC 3

╄→гoц情女王★ 提交于 2019-12-24 19:53:10

问题


I have implemented Identity in Asp.Net.Core MVC 3 because we already have database tables that we want to use. I get the error when calling the methods below and they are both happening in the UserManager in the same call to the method. When looking at the source code for this method it is because the Store variable is null. But I am not quite sure how to make sure it is not null. I have looked at various solutions online but none solve my problem.

My solution is working with the SignInManager for passwords but I can not get it to get passed the error for Roles.

ConfigureServices Method

public void ConfigureServices(IServiceCollection services)
{           
    services.AddControllersWithViews();
    services.AddMvc(options =>
    {
        var policy = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build();
        options.Filters.Add(new AuthorizeFilter(policy));
        options.EnableEndpointRouting = false;
    });
    services.AddDbContext<EntitiesModel>(options => options.UseSqlServer(
            Configuration["Data:ConnectionStrings:XXXXXXXXXXXXX"]));
    services.AddIdentity<UserViewModel, UserRoleViewModel>().AddDefaultTokenProviders();
    services.AddTransient<IUserStore<UserViewModel>, UserStore>();
    services.AddTransient<IRoleStore<UserRoleViewModel>, RoleStore>();
    services.ConfigureApplicationCookie(options =>
    {
        options.Cookie.HttpOnly = true;             
        options.LoginPath = "/Login";
        options.LogoutPath = "/Logout";
    });         
}

UserStore class

public class UserStore : IUserStore<UserViewModel>, IUserPasswordStore<UserViewModel>, IUserEmailStore<UserViewModel>, IUserRoleStore<UserRoleViewModel>
{ 
....

RoleStore class

public class RoleStore : IRoleStore<UserRoleViewModel>,IUserRoleStore<UserRoleViewModel>
{
....

UserViewModel class

public class UserViewModel 
{
    [Key, Required]
    public int Id { get; set; }
    [Required, MaxLength(128)]
    public string UserName { get; set; }
    [Required, MaxLength(1024)]
    public string Password { get; set; }        
    public virtual ICollection<UserRoleViewModel> UserRoles { get; set; }
}

UserRoleViewRoleModel class

public class UserRoleViewModel 
{    
    [Key, Required]
    public int Id { get; set; }
    [Required, ForeignKey(nameof(User))]
    public int UserId { get; set; }
    [Required, ForeignKey(nameof(Role))]
    public int RoleId { get; set; }
    public virtual RoleViewModel Role { get; set; }
    public virtual UserViewModel User { get; set; }
}

RoleViewModel class

public class RoleViewModel 
{   
    [Key,Required]
    public int RoleId  { get; set; }
    [Required]
    public string RoleName { get; set; }
    public virtual  ICollection<UserRoleViewModel> UserRoles { get; set; }
}

**Calling Code**
//this works
var result = await signInManager.PasswordSignInAsync(user.UserName, user.Password, true, false);
//this fails
var test = await userManager.IsInRoleAsync(user, "Management");
//this fails
var roles = await userManager.GetRolesAsync(user);

Error

NotSupportedException: Store does not implement IUserRoleStore. Microsoft.AspNetCore.Identity.UserManager.GetUserRoleStore()

Forgive me if the way I have structured this is not right but I am new to this coming from ASP.Net Web Forms


回答1:


Not Sure but it seems you forgot to add Role in start up.cs file

    services.AddDefaultIdentity<UserViewModel>()
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<DbContext>();

Please find below link for more reference. http://qaru.site/questions/16512353/store-does-not-implement-iuserrolestoretuser-aspnet-core-21-identity



来源:https://stackoverflow.com/questions/58654400/store-does-not-implement-iuserrolestoretuser-usermanagertuser-getuserrolesto

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!