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

后端 未结 1 1922
夕颜
夕颜 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
    {
        [Required]
        [StringLength(50)]
        public string FirstName { get; set; }
    
        [Required]
        [StringLength(50)]
        public string LastName { get; set; }
    
        public async Task 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
    {
    }
    
    public class ApplicationUserClaim : IdentityUserClaim
    {
    }
    
    public class ApplicationUserRole : IdentityUserRole
    {
    }
    
    public class ApplicationRole : IdentityRole
    {
        [Required]
        [StringLength(50)]
        public string ProperName { get; set; }
    }
    
    
    public class MyAppDb : IdentityDbContext
    {
        public static MyAppDb Create()
        {
            return new MyAppDb();
        }
    
        public MyAppDb()
            : base("MyAppDb")
        {
        }
    }
    
    
    public class ApplicationUserStore : UserStore
    {
        public ApplicationUserStore(MyAppDb context)
            : base(context)
        {
        }
    }
    
    public class ApplicationUserManager : UserManager
    {
        public ApplicationUserManager(IUserStore store)
            : base(store)
        {
        }
    
        public static ApplicationUserManager Create(IdentityFactoryOptions options)
        {
            var manager = new ApplicationUserManager(new ApplicationUserStore(new MyAppDb()));
            // Configure the application user manager
            manager.UserValidator = new UserValidator(manager)
            {
                AllowOnlyAlphanumericUserNames = false,
                RequireUniqueEmail = true
            };
            manager.PasswordValidator = (IIdentityValidator)new MinimumLengthValidator(8);
    
            return manager;
        }
    }
    
    public class ApplicationRoleStore : RoleStore
    {
        public ApplicationRoleStore(MyAppDb context)
            : base(context)
        {
        }
    }
    
    public class ApplicationRoleManager : RoleManager
    {
        public ApplicationRoleManager(IRoleStore store)
            : base(store)
        {
        }
    
        public static ApplicationRoleManager Create(IdentityFactoryOptions options)
        {
            var manager = new ApplicationRoleManager(new ApplicationRoleStore(new MyAppDb()));
    
            return manager;
        }
    }
    

    0 讨论(0)
提交回复
热议问题