问题描述
我正在使用ASPNet Identity来实现Web应用程序中的安全性.
I am using ASPNet Identity to implement security in my web application.
有一个需求,我需要扩展IdentityRole和IdentityUser.
There is a requirements where in, I need to extend the IdentityRole and IdentityUser.
这是我的代码,用于扩展IdentityUser.
Here is my code to extend the IdentityUser.
public class ApplicationUser : IdentityUser
{
public virtual User User { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("name=CoreContext")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<IdentityUser>()
.ToTable("AspNetUsers");
modelBuilder.Entity<ApplicationUser>()
.ToTable("AspNetUsers");
}
}
我唯一的问题是IdentityRole
My only problem is the IdentityRole
推荐答案
要扩展User,请将您的ApplicationUser
类(位于IdentityModels.cs
中)更新为
To extend User , Update your ApplicationUser
class(located in IdentityModels.cs
) to
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity>
GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
var userIdentity = await manager
.CreateIdentityAsync(this,
DefaultAuthenticationTypes.ApplicationCookie);
return userIdentity;
}
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
// Use a sensible display name for views:
[Display(Name = "Postal Code")]
public string PostalCode { get; set; }
}
要扩展角色,请创建一个类ApplicationRole.cs
To Extend Role , Create a class ApplicationRole.cs
public class ApplicationRole : IdentityRole
{
public ApplicationRole() : base() { }
public ApplicationRole(string name) : base(name) { }
public string Description { get; set; }
}
并将类添加到IdentityConfig.cs
文件中:
public class ApplicationRoleManager : RoleManager<ApplicationRole>
{
public ApplicationRoleManager(
IRoleStore<ApplicationRole,string> roleStore)
: base(roleStore)
{
}
public static ApplicationRoleManager Create(
IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)
{
return new ApplicationRoleManager(
new RoleStore<ApplicationRole>(context.Get<ApplicationDbContext>()));
}
}
现在清除旧数据库,运行该应用程序并注册一个用户.它将在AspNetUsers表中再创建3个字段(地址,城市,州),并在AspNetRoles表中再创建一个字段(说明).就是这样.
Now clear the old database, Run the application and register a user. It will create 3 more fields(Address,City,State) in AspNetUsers table and one more field(Description) into AspNetRoles table. That's it.
有关更多信息,请访问网站:扩展身份角色
For more info , go to the site :Extending Identity Role
这篇关于扩展IdentityRole和IdentityUser的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!