Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 913
Darren Avatar asked Mar 04 '23 05:03

Darren


1 Answers

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

like image 164
Ravi Mali Avatar answered Mar 22 '23 07:03

Ravi Mali