我正在尝试使用ASP.NET Identity 2.1、ASP.NET Web API 2.2构建成员资格系统,我需要扩展IdentityUserRole
以将另一个PK添加到其表中,如下所示:
public class ApplicationUserRoles : IdentityUserRole
{
public Guid ProjectId { get; set; }
public Project Project { get; set; }
}
public class Project
{
public Project()
{
ProjectId = new Guid();
}
public string ProjectName { get; set; }
[Key]
public Guid ProjectId { get; set; }
}
在DBcontext类中,我添加了:
protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder); // This needs to go before the other rules!
modelBuilder.Entity<ApplicationUserRoles>().HasKey(m => new {m.ProjectId, m.UserId, m.RoleId}).ToTable("ApplicationUserRoles");
}
public DbSet<ApplicationUserRoles> ApplicationUserRoles { get; set; }
这根本不起作用,我没有做错什么,添加迁移保持生成:
CreateTable(
"dbo.ApplicationUserRoles",
c => new
{
UserId = c.String(nullable: false, maxLength: 128),
RoleId = c.String(nullable: false, maxLength: 128),
ProjectId = c.Guid(nullable: false),
})
.PrimaryKey(t => new { t.UserId, t.RoleId })
.ForeignKey("dbo.AspNetUserRoles", t => new { t.UserId, t.RoleId })
.ForeignKey("dbo.Projects", t => t.ProjectId, cascadeDelete: true)
.Index(t => new { t.UserId, t.RoleId })
.Index(t => t.ProjectId);
}
正如您所看到的PrimaryKey(t => new { t.UserId, t.RoleId })
。我需要它是PrimaryKey(t => new { t.ProjectId,t.UserId, t.RoleId })
我终于成功了,我在这里做了什么:
在IdentityUser的签名处myApplicationUser从其继承
public class IdentityUser<TKey, TLogin, TRole, TClaim> : IUser<TKey>
where TLogin : Microsoft.AspNet.Identity.EntityFramework.IdentityUserLogin<TKey>
where TRole : Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole<TKey>
where TClaim : Microsoft.AspNet.Identity.EntityFramework.IdentityUserClaim<TKey>
{
}
它接受我对IdentityUserRole的自定义定义。
我的类ApplicationUser我需要实现正确的类型:
public class ApplicationUser : IdentityUser<string, IdentityUserLogin, ApplicationUserSecurityProfile, IdentityUserClaim>
{
}
以及我的角色:
public class ApplicationSecurityProfile : IdentityRole<string, ApplicationUserSecurityProfile>
{
}
和我的ApplicationDbContext:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationSecurityProfile, string, IdentityUserLogin, ApplicationUserSecurityProfile, IdentityUserClaim>, IContext
{
}
和我的UserStore:
public class ApplicationUserStore : UserStore<ApplicationUser, ApplicationSecurityProfile, string, IdentityUserLogin, ApplicationUserSecurityProfile, IdentityUserClaim>, IUserStore<ApplicationUser>
{
public ApplicationUserStore ApplicationDbContext context)
: base(context)
{
}
}
和myRoleStore:
public class ApplicationSecurityProfileStore : RoleStore<ApplicationSecurityProfile, string, ApplicationUserSecurityProfile>
{
public ApplicationSecurityProfileStore(ApplicationDbContext context)
: base(context)
{
}
}
然后我在我的自定义管理器中使用了我的自定义类:
RoleManger:
public class ApplicationSecurityProfileManager : RoleManager<ApplicationSecurityProfile>
{
private ApplicationDbContext db = new ApplicationDbContext();
public ApplicationSecurityProfileManager(IRoleStore<ApplicationSecurityProfile, string> roleStore)
: base(roleStore)
{
}
public static ApplicationSecurityProfileManager Create(IdentityFactoryOptions<ApplicationSecurityProfileManager> options, IOwinContext context)
{
var appRoleManager = new ApplicationSecurityProfileManager(new ApplicationSecurityProfileStore(context.Get<ApplicationDbContext>()));
return appRoleManager;
}
}
我的UserManager
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(ApplicationUserStore store)
: base(store)
{
}
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var appDbContext = context.Get<ApplicationDbContext>();
var appUserManager = new ApplicationUserManager(new ApplicationUserStore(appDbContext));
// Configure validation logic for usernames
appUserManager.UserValidator = new UserValidator<ApplicationUser>(appUserManager)
{
AllowOnlyAlphanumericUserNames = true,
RequireUniqueEmail = true
};
// Configure validation logic for passwords
appUserManager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = true,
RequireDigit = false,
RequireLowercase = true,
RequireUppercase = true,
};
appUserManager.EmailService = new AspNetIdentity.WebApi.Services.EmailService();
var dataProtectionProvider = options.DataProtectionProvider;
if (dataProtectionProvider != null)
{
appUserManager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"))
{
//Code for email confirmation and reset password life time
TokenLifespan = TimeSpan.FromHours(6)
};
}
return appUserManager;
}
}
并且它最终工作