Why is UserManager.CreateIdentityAsync() looking for IdentityRole and how to fix?

后端 未结 1 1904
夕颜
夕颜 2020-12-31 21:37

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

相关标签:
1条回答
  • 2020-12-31 22:06

    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;
        }
    }
    
    0 讨论(0)
提交回复
热议问题