问题描述
我试图扩展Web API 2(个人账户)中提供的ASPNET IdentityRole类模板在Visual Studio 2013的最新版本,当我打/ API /角色则返回一个空数组
I am attempting to extend the AspNet IdentityRole class provided in the Web API 2 (With Individual Accounts) template in the latest version of Visual Studio 2013. When I hit /api/roles it returns an empty array
身份识别模型
namespace API.Models
{
// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
public class ApplicationUser : IdentityUser<string, IdentityUserLogin, ApplicationUserRole, IdentityUserClaim>, IUser, IUser<string>
{
[NotMapped]
public virtual UserProfile Profile { get; set; }
public virtual ICollection<ApplicationUserRole> UserRoles { get; set; }
//public virtual List<ApplicationUserRole> ApplicationRoles { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager,
string authenticationType)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
ClaimsIdentity userIdentity = await manager.CreateIdentityAsync(this, authenticationType);
// Add custom user claims here
return userIdentity;
}
}
public class ApplicationRole : IdentityRole<string, ApplicationUserRole>
{
public ApplicationRole() : base() { }
}
public class ApplicationUserRole : IdentityUserRole<string>
{
public ApplicationUserRole()
: base()
{ }
public virtual ApplicationRole Role { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser,ApplicationRole,string,IdentityUserLogin,ApplicationUserRole,IdentityUserClaim>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
base.Configuration.ProxyCreationEnabled = false;
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
}
身份配置
namespace API
{
// 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<ApplicationUser, string>
{
public ApplicationUserManager(IUserStore<ApplicationUser> store)
: base(store)
{
}
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options,
IOwinContext context)
{
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
// Configure validation logic for usernames
manager.UserValidator = new UserValidator<ApplicationUser>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
// Configure validation logic for passwords
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = true,
RequireDigit = true,
RequireLowercase = true,
RequireUppercase = true,
};
IDataProtectionProvider dataProtectionProvider = options.DataProtectionProvider;
if (dataProtectionProvider != null)
{
manager.UserTokenProvider =
new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
}
return manager;
}
}
public class ApplicationRoleManager : RoleManager<ApplicationRole, string>
{
public ApplicationRoleManager(IRoleStore<ApplicationRole, string> store)
: base(store)
{
}
public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options,
IOwinContext context)
{
var manager = new ApplicationRoleManager(new RoleStore<ApplicationRole>(context.Get<ApplicationDbContext>()));
return manager;
}
}
}
推荐答案
你错就错在这里。
ApplicationDbContext : IdentityDbContext<ApplicationUser>
你的数据库上下文是从某种程度上 IdentityDbContext&LT; ApplicationUser&GT;
这是基地
IdentityDbContext<TUser, IdentityRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>
ApplicationDbContext将产生 IdentityRole
时actualy你想 ApplicationRole
为你的角色。
ApplicationDbContext will generate IdentityRole
when actualy you want ApplicationRole
as your role.
您ApplicationDbContext改为
Change your ApplicationDbContext to
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, string, IdentityUserLogin, ApplicationUserRole, IdentityUserClaim>
{
public ApplicationDbContext()
: base("DefaultConnection", false)
{
base.Configuration.ProxyCreationEnabled = false;
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
因为你在ApplicationUserRole有导航属性你应该添加configurationmapping
since you have navigation property in ApplicationUserRole you should add configurationmapping
protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ApplicationUserRole>().HasRequired(ur => ur.Role).WithMany().HasForeignKey(ur => ur.RoleId);
}
IdentityModel.cs应该是这样的:
IdentityModel.cs should be like this :
public class ApplicationUserRole : IdentityUserRole<string>
{
public ApplicationUserRole()
: base()
{ }
public virtual ApplicationRole Role { get; set; }
}
public class ApplicationRole : IdentityRole<string, ApplicationUserRole>
{
public ApplicationRole() : base() { }
public ApplicationRole(string name, string description)
: base()
{
this.Name = name;
this.Description = description;
}
public string Description { get; set; }
}
public class ApplicationUser : IdentityUser<string, IdentityUserLogin, ApplicationUserRole, IdentityUserClaim>
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
return userIdentity;
}
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, string, IdentityUserLogin, ApplicationUserRole, IdentityUserClaim>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
base.Configuration.ProxyCreationEnabled = false;
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ApplicationUserRole>().HasRequired(ur => ur.Role).WithMany().HasForeignKey(ur => ur.RoleId);
}
}
identityconfig.cs应该是这样的:
identityconfig.cs should be like this:
public class ApplicationUserStore : UserStore<ApplicationUser, ApplicationRole, string, IdentityUserLogin, ApplicationUserRole, IdentityUserClaim>, IUserStore<ApplicationUser>
{
public ApplicationUserStore(ApplicationDbContext context)
: base(context)
{
}
}
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(IUserStore<ApplicationUser> store)
: base(store)
{
}
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var manager = new ApplicationUserManager(new ApplicationUserStore(context.Get<ApplicationDbContext>()));
....
}
}
public class ApplicationRoleStore : RoleStore<ApplicationRole, string, ApplicationUserRole>
{
public ApplicationRoleStore(ApplicationDbContext context)
: base(context)
{
}
}
public class ApplicationRoleManager : RoleManager<ApplicationRole, string>
{
public ApplicationRoleManager(IRoleStore<ApplicationRole, string> store)
: base(store)
{
}
public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options,
IOwinContext context)
{
var manager = new ApplicationRoleManager(new ApplicationRoleStore(context.Get<ApplicationDbContext>()));
return manager;
}
}
这篇关于我如何使用Web API 2 + ASPNET身份2延伸IdentityRole的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!