商店未实现IUserRoleStore< TUser>UserManager< TUser> .GetUserRoleStore()ASP.NET Core MVC 3 [英] Store does not implement IUserRoleStore<TUser> UserManager<TUser>.GetUserRoleStore() ASP.NET Core MVC 3

查看:70
本文介绍了商店未实现IUserRoleStore< TUser>UserManager< TUser> .GetUserRoleStore()ASP.NET Core MVC 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在 Asp.Net.Core MVC 3 中实现了 Identity ,因为我们已经有了要使用的数据库表.我在调用下面的方法时收到错误,并且它们都在同一方法的 UserManager 中发生.在查看此方法的源代码时,这是因为Store变量为null.但是我不太确定如何确保它不为空.我在网上查看了各种解决方案,但没有一个能解决我的问题.

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.

我的解决方案正在与 SignInManager 一起使用密码,但是我无法通过它来传递Roles错误.

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

ConfigureServices 方法

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

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

RoleStore

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

UserViewModel

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

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

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);

错误

NotSupportedException:存储未实现IUserRoleStore.Microsoft.AspNetCore.Identity.UserManager.GetUserRoleStore()

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

如果我的结构方式不正确,请原谅我,但我是来自ASP.Net Web Forms的新手.

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

推荐答案

不确定,但是您似乎忘记了在start.cs文件中添加角色

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

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

请在下面的链接中找到更多参考. http://qaru.site/questions/16512353/store-does-not-implement-iuserrolestoretuser-aspnet-core-21-identity

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

这篇关于商店未实现IUserRoleStore&lt; TUser&gt;UserManager&lt; TUser&gt; .GetUserRoleStore()ASP.NET Core MVC 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆