【问题标题】:Correct syntax for seeding role/users after changing primary key for users更改用户主键后播种角色/用户的正确语法
【发布时间】:2017-10-23 22:27:01
【问题描述】:

使用默认的 ASP.NET MVC 5 项目,我可以使用以下代码播种和角色。在为用户自定义主键后,我正在努力让语法正确以更新它。

我在配置类中遇到 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>'

如何更正此代码以说明对 CustomUserStore 和 CustomRoleStore 的更改?

主键更改基于此Change Primary Key for Users in 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);
            }
        }
    }
}

使其适用于以下自定义

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

【问题讨论】:

    标签: c# asp.net-mvc


    【解决方案1】:

    典型的,发布后尽快修复它。

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

    【讨论】:

      猜你喜欢
      • 2012-11-13
      • 2021-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-10
      • 2012-08-23
      • 2020-02-25
      • 1970-01-01
      相关资源
      最近更新 更多