为什么我得到一个IdentityRole错误

为什么我得到一个IdentityRole错误

本文介绍了为什么我得到一个IdentityRole错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Identity 2.0注册用户后,用户将在数据库中创建,帐号控制器中有代码来创建身份并对用户进行登录。这是当我收到错误。以下是产生错误的代码:

Using Identity 2.0 After registering a user, the user is created in the database, there is code in the Account controller to create an identity and sign the user in. This is when I get the error. Here is the code producing the error:

var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);

这里是错误:

我的模型看起来像这样:

My Model looks like this:

namespace MyApp.Models
{
    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")
        {
        }
    }
}

我的 UserManager 在帐户控制器中创建:

My UserManager is created like this in the Account controller:

namespace MyApp.Controllers
{
    [Authorize]
    public class AccountController : BaseController
    {
        private UserManager<ApplicationUser> _userManager;

        public AccountController()
        {
        }

        public AccountController(UserManager<ApplicationUser> userManager)
        {
            UserManager = userManager;
        }

        public UserManager<ApplicationUser> UserManager
        {
            get
            {
                return _userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new MyAppDb()));
            }
            private set
            {
                _userManager = value;
            }
        }

        ...
    }
}


推荐答案

我花了很多时间在这个问题上发表了许多问题,以达到它的底线。我确定希望此信息可以帮助其他人。

I've spent a lot of time on this issue and posted many questions to get to the bottom of it. I sure hope this information helps other folks.

基本上,定制用户很容易,有几个示例可用。然而,定制角色是有问题的,我发现的唯一示例被定制远远超出了开箱即用的内容,并且我能够在解决 Id 问题。在这种情况下的问题是我必须自己创建 Id 。以下是这个问题:

Basically, customizing the User is easy and there are several examples available. However, customizing the Role is problematic and the only examples I found were customized far beyond what comes "out of the box" and I was able to get one working after resolving an Id issue. The issue in that case was that I had to create the Id myself. Here is that issue: UserManager.Create(user, password) thowing EntityValidationError saying Id is required?

所以在简单的例子中,当你只是定制 User 您的 DbContext 继承自 IdentityDbContext ,您只需提供 IdentityUser ,在我的情况下,它被称为 ApplicationUser ,看起来像这样 IdentityDbContext< ApplicationUser> 。还要注意, UserManager UserStore 提供了 IdentityUser 并且在大多数示例中看起来像这样 new UserManager< ApplicationUser>(new UserStore< ApplicationUser>(new ApplicationDbContext()))

So in simple examples, notice when you are just customizing the User your DbContext inherits from IdentityDbContext and you just provide the IdentityUser, in my case it was called ApplicationUser and looked like this IdentityDbContext<ApplicationUser>. Also notice that the UserManager and UserStore is provided the IdentityUser as well, and in most examples looks like this new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()))

但是,一旦您自定义角色 ,您需要传递所有内容,包括密钥类型 TKey Id 字段。因为我只定制了用户,因此我看起来像这样 IdentityDbContext< ApplicationUser,ApplicationRole,string,IdentityUserLogin,IdentityUserRole,IdentityUserClaim> 角色

But once you customize your Role you need to pass everything, including the key type TKey of the Id fields. Mine looks like this IdentityDbContext<ApplicationUser, ApplicationRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim> because I only customized the User and Role.

但是你还没有完成,因为常规的 UserManager 不了解了。我认为这是身份人可以/应该解决的问题,但我离题。所以我必须实现自己的 UserStore UserManager 并注意(这是让我失望的部分)你必须提供 KType 或者实例化您的自定义 UserManager 将给您一个编译时错误:

But you aren't done yet, because the regular UserManager doesn't understand anymore. I think this is something the Identity folks could/should fix, but I digress. So I had to implement my own UserStore and UserManager and notice (and this is the part that tripped me up) you have to provide the KType here too or instantiating your custom UserManager will give you a compile-time error:

public class ApplicationUserStore : UserStore<ApplicationUser, ApplicationRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>
{
    public ApplicationUserStore(MyAppDb context)
        : base(context)
    {
    }
}

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

并在帐户控制器中实例化 UserManager ,这样 new ApplicationUserManager(new ApplicationUserStore(new MyAppDb()))

And instantiate the UserManager in the Account controller like this new ApplicationUserManager(new ApplicationUserStore(new MyAppDb())):

public class AccountController : BaseController
{
    private ApplicationUserManager _userManager;

    public AccountController()
    {
    }

    public AccountController(ApplicationUserManager userManager)
    {
        UserManager = userManager;
    }

    public ApplicationUserManager UserManager
    {
        get
        {
            return _userManager = new ApplicationUserManager(new ApplicationUserStore(new MyAppDb()));
        }
        private set
        {
            _userManager = value;
        }
    }

    ...

}

希望这有帮助!

这篇关于为什么我得到一个IdentityRole错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 09:49