为什么 UserManager.CreateIdentityAsync() 正在寻找 IdentityRole 以及如何解决? [英] Why is UserManager.CreateIdentityAsync() looking for IdentityRole and how to fix?

查看:18
本文介绍了为什么 UserManager.CreateIdentityAsync() 正在寻找 IdentityRole 以及如何解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Identity2.0MVC5 CodeFirst 我已经扩展了 IdentityUserIdentityRole 像这样:

I'm using Identity2.0 with MVC5 CodeFirst I have extended both the IdentityUser and IdentityRole like this:

public class ApplicationUser : IdentityUser
{
    [Required]
    [StringLength(50)]
    public string FirstName { get; set; }

    [Required]
    [StringLength(50)]
    public string LastName { get; set; }
}

public class ApplicationRole : IdentityRole
{
    [Required]
    [StringLength(50)]
    public string ProperName { get; set; }

    [Required]
    public string Description { get; set; }
}

public class MyAppDb : IdentityDbContext<ApplicationUser, ApplicationRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>
{
    public MyAppDb()
        : base("MyAppDb")
    {
    }
}

注意 MyAppDb 继承自 IdentityDbContext 但我传递的是 ApplicationRole 而不是 IdentityRole.我的 ApplicationRole 继承自 IdentityRole 所以这应该不是问题.

Notice MyAppDb inherits from IdentityDbContext but that I pass an ApplicationRole instead of IdentityRole. My ApplicationRole inherits from IdentityRole so this should not be a problem.

但是……

我的 AccountController 抛出此错误:

The entity type IdentityRole is not part of the model for the current context. 

在此代码中的UserManager.CreateIdentityAsync(...)处:

private async Task SignInAsync(ApplicationUser user, bool isPersistent)
{
     AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
     var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
     AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
}

用户正在这里创建:

        var user = await UserManager.FindAsync(model.UserName, model.Password);
        if (user != null)
        {
            await SignInAsync(user, model.RememberMe);
            return RedirectToLocal(returnUrl);
        }

什么是重大故障"?;)

What is the "major malfunction"? ;)

更新:我很确定这个问题与这里的 UserStore 有关:

UPDATE: I'm fairly sure the issue has something to do with the UserStore here:

    public class AccountController : Controller
    {
        public AccountController ()
            : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new MyAppDb())))
        {
        }

        public AccountController (UserManager<ApplicationUser> userManager)
        {
            UserManager = userManager;
            UserManager.PasswordValidator = (IIdentityValidator<string>)new MinimumLengthValidator(8);
            UserManager.UserValidator = new UserValidator<ApplicationUser>(UserManager) { AllowOnlyAlphanumericUserNames = false };
        }

        public UserManager<ApplicationUser> UserManager { get; private set; }
...

当我只能传递一个参数时,如何让 UserStore 了解 ApplicationRole?

How do I get the UserStore to know about ApplicationRole when I can only pass it one argument?

推荐答案

我通过创建 ApplicationUserStoreApplicationUserManager 解决了这个问题:

I solved this issue by creating an ApplicationUserStore and ApplicationUserManager:

public class ApplicationUser : IdentityUser<string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
    [Required]
    [StringLength(50)]
    public string FirstName { get; set; }

    [Required]
    [StringLength(50)]
    public string LastName { get; set; }

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(ApplicationUserManager manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }

}

public class ApplicationUserLogin : IdentityUserLogin<string>
{
}

public class ApplicationUserClaim : IdentityUserClaim<string>
{
}

public class ApplicationUserRole : IdentityUserRole<string>
{
}

public class ApplicationRole : IdentityRole<string, ApplicationUserRole>
{
    [Required]
    [StringLength(50)]
    public string ProperName { get; set; }
}


public class MyAppDb : IdentityDbContext<ApplicationUser, ApplicationRole, string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
    public static MyAppDb Create()
    {
        return new MyAppDb();
    }

    public MyAppDb()
        : base("MyAppDb")
    {
    }
}


public class ApplicationUserStore : UserStore<ApplicationUser, ApplicationRole, string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
    public ApplicationUserStore(MyAppDb context)
        : base(context)
    {
    }
}

public class ApplicationUserManager : UserManager<ApplicationUser, string>
{
    public ApplicationUserManager(IUserStore<ApplicationUser, string> store)
        : base(store)
    {
    }

    public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options)
    {
        var manager = new ApplicationUserManager(new ApplicationUserStore(new MyAppDb()));
        // Configure the application user manager
        manager.UserValidator = new UserValidator<ApplicationUser>(manager)
        {
            AllowOnlyAlphanumericUserNames = false,
            RequireUniqueEmail = true
        };
        manager.PasswordValidator = (IIdentityValidator<string>)new MinimumLengthValidator(8);

        return manager;
    }
}

public class ApplicationRoleStore : RoleStore<ApplicationRole, string, ApplicationUserRole>
{
    public ApplicationRoleStore(MyAppDb context)
        : base(context)
    {
    }
}

public class ApplicationRoleManager : RoleManager<ApplicationRole, string>
{
    public ApplicationRoleManager(IRoleStore<ApplicationRole, string> store)
        : base(store)
    {
    }

    public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options)
    {
        var manager = new ApplicationRoleManager(new ApplicationRoleStore(new MyAppDb()));

        return manager;
    }
}

这篇关于为什么 UserManager.CreateIdentityAsync() 正在寻找 IdentityRole 以及如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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