为什么我会得到一个错误IdentityRole? [英] Why am I getting an IdentityRole error?

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

问题描述

使用Identity 2.0注册用户后

,在数据库中创建的用户,没有在账户控制器创建一个身份和登录用户code,这是我得到的错误。这里是code产生错误:

  VAR身份=等待UserManager.CreateIdentityAsync(用户,DefaultAuthenticationTypes.ApplicationCookie);

这里是错误:


  

的实体类型IdentityRole不是模型对当前的一部分
  上下文。


我的模型是这样的:

 命名空间MyApp.Models
{
    公共类ApplicationUser:IdentityUser
    {
        [需要]
        [StringLength(50)]
        公共字符串名字{获得;组; }        [需要]
        [StringLength(50)]
        公共字符串名字{获得;组; }
    }    公共类ApplicationRole:IdentityRole
    {
        [需要]
        [StringLength(50)]
        公共字符串ProperName {搞定;组; }        [需要]
        公共字符串描述{搞定;组; }
    }
    公共类MyAppDb:IdentityDbContext< ApplicationUser,ApplicationRole,串,IdentityUserLogin,IdentityUserRole,IdentityUserClaim>
    {
        公共MyAppDb()
            :基地(MyAppDb)
        {
        }
    }
}

我的的UserManager 是这样的账户控制器中创建:

 命名空间MyApp.Controllers
{
    [授权]
    公共类的AccountController:BaseController
    {
        私人的UserManager< ApplicationUser> _userManager;        公众的AccountController()
        {
        }        公众的AccountController(的UserManager< ApplicationUser>的UserManager)
        {
            的UserManager =的UserManager;
        }        公众的UserManager< ApplicationUser>的UserManager
        {
            得到
            {
                返回_userManager =新的UserManager&所述; ApplicationUser>(新UserStore&所述; ApplicationUser>(新MyAppDb()));
            }
            私订
            {
                _userManager =价值;
            }
        }        ...
    }
}


解决方案

我花了很多时间在这个问题上,并张贴许多问题要得到它的底部。我当然希望这个信息可以帮助其他人。

基本上,定制用户是容易的,有几个可用的例子。然而,自定义角色是有问题的,我发现的唯一的例子是定制的,远远超过了来自开箱即用,我能解决一个后得到一个工作编号问题。在这种情况下的问题是,我不得不创建编号自己。下面是这个问题:<一href=\"http://stackoverflow.com/questions/22698744/usermanager-createuser-password-thowing-entityvalidationerror-saying-id-is-re\">UserManager.Create(user,密码)异常触发EntityValidationError说时需出示身份证?

因此​​,在简单的例子,注意当你刚自定义用户从<$ C $你的的DbContext 继承C> IdentityDbContext ,您只需提供 IdentityUser ,在我的情况下,它被称为 ApplicationUser 看着这样的 IdentityDbContext&LT; ApplicationUser&GT; 。还要注意,的UserManager UserStore 提供了 IdentityUser 同时,在大多数例子看起来像这样新的UserManager&LT; ApplicationUser&GT;(新UserStore&LT; ApplicationUser&GT;(新ApplicationDbContext()))

但是,一旦你自定义您的角色 你需要传递的一切,包括密钥类型 TKEY的编号字段。我的是这样 IdentityDbContext&LT; ApplicationUser,ApplicationRole,串,IdentityUserLogin,IdentityUserRole,IdentityUserClaim&GT; 因为我只定制了用户角色

但你还没有完成,因为正规的UserManager 已经不明白。的我想这是身份的乡亲可以/应该解决,但我离题。的所以我必须实现我自己的 UserStore 的UserManager 和通知(这是我绊倒的部分)您必须提供 KType 点击这里太或实例化自定义的的UserManager 会给你一个编译时错误:

 公共类ApplicationUserStore:UserStore&LT; ApplicationUser,ApplicationRole,串,IdentityUserLogin,IdentityUserRole,IdentityUserClaim&GT;
{
    公共ApplicationUserStore(MyAppDb上下文)
        :基座(上下文)
    {
    }
}公共类ApplicationUserManager:&的UserManager LT; ApplicationUser,串&GT;
{
    公共ApplicationUserManager(IUserStore&LT; ApplicationUser,串&GT;存储)
        :基地(存储)
    {
    }
}

和实例化的UserManager 在账户控制器这样的新ApplicationUserManager(新ApplicationUserStore(新MyAppDb()))

 公共类的AccountController:BaseController
{
    私人ApplicationUserManager _userManager;    公众的AccountController()
    {
    }    公众的AccountController(ApplicationUserManager的UserManager)
    {
        的UserManager =的UserManager;
    }    公共ApplicationUserManager的UserManager
    {
        得到
        {
            返回_userManager =新ApplicationUserManager(新ApplicationUserStore(新MyAppDb()));
        }
        私订
        {
            _userManager =价值;
        }
    }    ...}

希望这有助于!

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

And here is the error:

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

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")
        {
        }
    }
}

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.

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?

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

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.

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)
    {
    }
}

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

    ...

}

Hope this helps!

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

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