【问题标题】:Why am I getting an IdentityRole error?为什么我收到 IdentityRole 错误?
【发布时间】:2014-04-01 21:01:34
【问题描述】:

使用 Identity 2.0 注册用户后,在数据库中创建了用户,Account 控制器中有代码用于创建身份并登录用户。这是我收到错误的时候。这是产生错误的代码:

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

这是错误:

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

我的模型如下所示:

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 在 Account 控制器中是这样创建的:

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

        ...
    }
}

【问题讨论】:

    标签: asp.net-mvc entity-framework entity-framework-5 asp.net-identity asp.net-mvc-5.1


    【解决方案1】:

    我在这个问题上花费了很多时间,并发布了许多问题来深入了解它。我当然希望这些信息对其他人有所帮助。

    基本上,自定义User 很容易,并且有几个示例可用。但是,自定义 Role 是有问题的,我发现的唯一示例是自定义的,远远超出了“开箱即用”的范围,并且在解决了 Id 问题后,我能够得到一个工作。这种情况下的问题是我必须自己创建Id。这是那个问题:UserManager.Create(user, password) thowing EntityValidationError saying Id is required?

    因此,在简单的示例中,请注意,当您自定义 User 时,您的 DbContext 继承自 IdentityDbContext,并且您只需提供 IdentityUser,在我的例子中它被称为 ApplicationUser,看起来像这样 @ 987654331@。另请注意,UserManagerUserStore 也提供了 IdentityUser,并且在大多数示例中看起来像这样 new UserManager&lt;ApplicationUser&gt;(new UserStore&lt;ApplicationUser&gt;(new ApplicationDbContext()))

    但是,一旦您自定义了 Role,您需要传递所有内容,包括 Id 字段的密钥类型 TKey。我的看起来像这样IdentityDbContext&lt;ApplicationUser, ApplicationRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim&gt;,因为我只自定义了UserRole

    但是你还没有完成,因为普通的UserManager 不再理解了。 我认为这是 Identity 人员可以/应该解决的问题,但我离题了。 所以我必须实现自己的 UserStoreUserManager 并注意(这是让我绊倒的部分) 您也必须在此处提供KType,否则实例化您的自定义UserManager 会出现编译时错误:

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

    并在 Account 控制器中实例化 UserManager,如下所示 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;
            }
        }
    
        ...
    
    }
    

    希望这会有所帮助!

    【讨论】:

    • 您好,我想每个开发人员都知道,经过大量研究,最终解决方案可能看起来非常简单,但是您介意扩展一下是什么让您达到这一点吗?
    • 当然,我会更新答案并对我认为正在发生的事情给出最好的解释。
    • 嗨@SeanNewcome,当我尝试实施您的解决方案时,我该如何更改我的 user.GenerateUserIdentityAsync((ApplicationUserManager)UserManager);到?
    猜你喜欢
    • 1970-01-01
    • 2020-11-05
    • 2015-01-30
    • 2014-02-10
    • 2015-06-27
    • 2013-07-31
    • 1970-01-01
    • 2018-05-22
    • 1970-01-01
    相关资源
    最近更新 更多