简体   繁体   English

更改用户的主键后,为播种角色/用户提供正确的语法

[英]Correct syntax for seeding role/users after changing primary key for users

Using the default ASP.NET MVC 5 project I can seed and roles using the following code. 使用默认的ASP.NET MVC 5项目,我可以使用以下代码为种子和角色。 I'm struggling to get the syntax correct to update this after I've customized the primary key for users. 在为用户自定义主键之后,我正在努力获取正确的语法来更新它。

I get 2 errors in the Configuration class 我在Configuration类中遇到2个错误

The type 'ApplicationUser' cannot be used as type parameter 'TUser' in the generic type or method 'UserStore<TUser>'. There is no implicit reference conversion from 'ApplicationUser' to 'Microsoft.AspNet.Identity.EntityFramework.IdentityUser'.

Argument 1: cannot convert from 'Microsoft.AspNet.Identity.EntityFramework.UserStore<ApplicationUser>' to 'Microsoft.AspNet.Identity.IUserStore<ApplicationUser, int>'

How can I correct this code to account for change to the CustomUserStore and CustomRoleStore? 如何更正此代码以说明对CustomUserStore和CustomRoleStore的更改?

The primary key changes are based on this Change Primary Key for Users in ASP.NET Identity 主键更改基于此ASP.NET Identity中用户的更改主键

namespace IR.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;

    internal sealed class Configuration : DbMigrationsConfiguration<ApplicationDbContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(ApplicationDbContext context)
        {

            if (!context.Roles.Any(r => r.Name == "Administrator"))
            {
                var store = new RoleStore<IdentityRole>(context);
                var manager = new RoleManager<IdentityRole>(store);
                var role = new IdentityRole { Name = "Administrator" };
                manager.Create(role);
            }


            SeedNewUser(context, "email@email.com", "****", "Administrator");

        }

        private async void SeedNewUser(ApplicationDbContext context, string email, string dummyPassword, string role)
        {
            if (!context.Users.Any(u => u.UserName == email))
            {
                var store = new UserStore<ApplicationUser>(context);
                var manager = new UserManager<ApplicationUser>(store);
                var user = new ApplicationUser { UserName = email, Email = email, LockoutEnabled = false, EmailConfirmed = true, SecurityStamp = Guid.NewGuid().ToString() };

                manager.Create(user, dummyPassword);
                manager.AddToRole(user.Id, role);
            }
        }
    }
}

to make it work with the following customization 使它与以下自定义一起使用

namespace IR.Models
{

    public class ApplicationUser : IdentityUser<int, CustomUserLogin, CustomUserRole, CustomUserClaim>
    {
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser, int> 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 CustomUserRole : IdentityUserRole<int> { }
    public class CustomUserClaim : IdentityUserClaim<int> { }
    public class CustomUserLogin : IdentityUserLogin<int> { }

    public class CustomRole : IdentityRole<int, CustomUserRole>
    {
        public CustomRole() { }
        public CustomRole(string name) { Name = name; }
    }

    public class CustomUserStore : UserStore<ApplicationUser, CustomRole, int, CustomUserLogin, CustomUserRole, CustomUserClaim>
    {
        public CustomUserStore(ApplicationDbContext context) : base(context)
        {
        }
    }

    public class CustomRoleStore : RoleStore<CustomRole, int, CustomUserRole>
    {
        public CustomRoleStore(ApplicationDbContext context) : base(context)
        {
        }
    }


    public class ApplicationDbContext : IdentityDbContext<ApplicationUser, CustomRole, int, CustomUserLogin, CustomUserRole, CustomUserClaim>
    {
        public ApplicationDbContext() : base("DefaultConnection")
        {
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
    }
}

Typical, manage to fix it soon after posting. 通常,在发布后尽快修复它。

protected override void Seed(ApplicationDbContext context)
{

    if (!context.Roles.Any(r => r.Name == "Administrator"))
    {
        var store = new CustomRoleStore(context);
        var manager = new RoleManager<CustomRole, int>(store);
        var role = new CustomRole { Name = "Administrator" };
        manager.Create(role);
    }

    SeedNewUser(context, "email@email.com", "****", "Administrator");

}

private async void SeedNewUser(ApplicationDbContext context, string email, string dummyPassword, string role)
{
    if (!context.Users.Any(u => u.UserName == email))
    {
        var store = new CustomUserStore(context);
        var manager = new ApplicationUserManager(store);
        var user = new ApplicationUser { UserName = email, Email = email, LockoutEnabled = false, EmailConfirmed = true, SecurityStamp = Guid.NewGuid().ToString() };

        manager.Create(user, dummyPassword);
        manager.AddToRole(user.Id, role);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM