用户角色/授权在ASP.NET标识中不起作用

在我们的DbContext.cs上有这个(模型构建器)代码

base.OnModelCreating(modelBuilder); modelBuilder.Entity().HasKey(l => l.UserId); modelBuilder.Entity().HasKey(r => r.Id); modelBuilder.Entity().HasKey(r => new { r.RoleId, r.UserId }); modelBuilder.Entity().ToTable("ApplicationUser"); 

授权/用户角色外,一切正常。

检查所有表后,我注意到IdentityUserRoles表创建了4列: RoleId,UserId,IdentityRole_Id和ApplicationUser_Id。

我发现, IdentityRole_Id和ApplicationUser_Id [外键]被映射或使用,而不是RoleId和UserId [主键]。 不幸的是,身份(Id)数据被插入到RoleId / UserId列中,IdenityRole_Id / ApplicationUser_Id默认为NULL

请帮忙。

我的代码:

 public class RqDbContext : DbContext { private const string ConnectionString = "RqDbContext"; public RqDbContext() : base(ConnectionString) { } public static RqDbContext Create() { return new RqDbContext(); } // ---------------------------------------------------------------------- // Data Tables // ---------------------------------------------------------------------- public DbSet Quotes { get; set; } public DbSet Bookings { get; set; } public DbSet CompanyAccounts { get; set; } // ---------------------------------------------------------------------- // Security // ---------------------------------------------------------------------- public DbSet ApplicationUserExtends { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity().HasKey(l => l.UserId); modelBuilder.Entity().HasKey(r => r.Id); modelBuilder.Entity().HasKey(r => new { r.RoleId, r.UserId }); modelBuilder.Entity().ToTable("ApplicationUser"); } } public partial class ApplicationUser : IdentityUser { public async Task GenerateUserIdentityAsync(UserManager 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; } //custom+ public virtual ApplicationUserExtend Extend { get; set; } } public class ApplicationUserExtend { public ApplicationUserExtend() { } [Key] [Display(Name="Id")] [XmlAttribute] public int Id { get; set; } [Display(Name="Account Id")] [XmlAttribute] public int AccountId { get; set; } [Display(Name="Active Account Id")] [XmlAttribute] public int ActiveAccountId { get; set; } } public class RqInitializer : System.Data.Entity.DropCreateDatabaseAlways { protected override void Seed(RqDbContext context) { var testData = ReadTestData(); AddIdentityRoles(context, testData); AddUsers(context, testData); MvcUtil.SaveChanges(context); } private void AddUsers(RqDbContext context, TestDataDo testData) { var userStore = new UserStore(context); var userManager = new UserManager(userStore); //Roles.Enabled("user","member"); var userIndex = 0; foreach (var applicationUser in testData.ApplicationUsers) { var user = new ApplicationUser { UserName = applicationUser.UserName, Email = applicationUser.Email, PhoneNumber = applicationUser.PhoneNumber }; if (userIndex > testData.ApplicationUserExtends.Count) { throw new Exception("Make sure you the number of rows in ApplicationUserExtends, matches the number of rows in Users"); } user.Extend = new ApplicationUserExtend { AccountId = testData.ApplicationUserExtends[userIndex++].AccountId }; userManager.Create(user, applicationUser.Password); //set User Role userManager.AddToRole(user.Id, applicationUser.Role); //context.Users.Add(user); } context.SaveChanges(); } private void AddIdentityRoles(RqDbContext context, TestDataDo testData) { var roleStore = new RoleStore(context); var roleManager = new RoleManager(roleStore); foreach (var role in testData.IdentityRoles) { var identity = new IdentityRole(role.Name); roleManager.Create(identity); } context.SaveChanges(); } public static TestDataDo ReadTestData() { var xml = GetResource("Rq.Web.App_Specification.Rq-TestData.xml"); return XmlUtil.SerializeFromString(xml); } private static string GetResource(string file) { var assembly = Assembly.GetExecutingAssembly(); return ResourceUtil.GetAsString(assembly, file); } } // Configure the application user manager used in this application. UserManager is defined in ASP.NET Identity and is used by the application. public class ApplicationUserManager : UserManager { public ApplicationUserManager(IUserStore store) : base(store) { } public static ApplicationUserManager Create(IdentityFactoryOptions options, IOwinContext context) { var manager = new ApplicationUserManager(new UserStore(context.Get())); // Configure validation logic for usernames manager.UserValidator = new UserValidator(manager) { AllowOnlyAlphanumericUserNames = false, RequireUniqueEmail = true }; // Configure validation logic for passwords manager.PasswordValidator = new PasswordValidator { RequiredLength = 6, RequireNonLetterOrDigit = true, RequireDigit = true, RequireLowercase = true, RequireUppercase = true, }; // Configure user lockout defaults manager.UserLockoutEnabledByDefault = true; manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5); manager.MaxFailedAccessAttemptsBeforeLockout = 5; // Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user // You can write your own provider and plug it in here. manager.RegisterTwoFactorProvider("Phone Code", new PhoneNumberTokenProvider { MessageFormat = "Your security code is {0}" }); manager.RegisterTwoFactorProvider("Email Code", new EmailTokenProvider { Subject = "Security Code", BodyFormat = "Your security code is {0}" }); manager.EmailService = new EmailService(); manager.SmsService = new SmsService(); var dataProtectionProvider = options.DataProtectionProvider; if (dataProtectionProvider != null) { manager.UserTokenProvider = new DataProtectorTokenProvider(dataProtectionProvider.Create("ASP.NET Identity")); } return manager; } } // Configure the application sign-in manager which is used in this application. public class ApplicationSignInManager : SignInManager { public ApplicationSignInManager(ApplicationUserManager userManager, IAuthenticationManager authenticationManager) : base(userManager, authenticationManager) { } public override Task CreateUserIdentityAsync(ApplicationUser user) { return user.GenerateUserIdentityAsync((ApplicationUserManager)UserManager); } public static ApplicationSignInManager Create(IdentityFactoryOptions options, IOwinContext context) { return new ApplicationSignInManager(context.GetUserManager(), context.Authentication); } } 

下面的代码将修复IdentityUserRoles表外键问题。

 var user = modelBuilder.Entity() .ToTable("AspNetUsers"); user.HasMany(u => u.Roles).WithRequired().HasForeignKey(ur => ur.UserId); user.HasMany(u => u.Claims).WithRequired().HasForeignKey(uc => uc.UserId); user.HasMany(u => u.Logins).WithRequired().HasForeignKey(ul => ul.UserId); user.Property(u => u.UserName).IsRequired(); modelBuilder.Entity() .HasKey(r => new { r.UserId, r.RoleId }) .ToTable("AspNetUserRoles"); modelBuilder.Entity() .HasKey(l => new { l.UserId, l.LoginProvider, l.ProviderKey}) .ToTable("AspNetUserLogins"); modelBuilder.Entity() .ToTable("AspNetUserClaims"); var role = modelBuilder.Entity() .ToTable("AspNetRoles"); role.Property(r => r.Name).IsRequired(); role.HasMany(r => r.Users).WithRequired().HasForeignKey(ur => ur.RoleId); 

我在这里找到了答案。 使用SQL脚本创建ASP.NET标识表 !