我正在嘗試擴展最新版本的Visual Studio 2013中的Web API 2(With Individual Accounts)模板中提供的AspNet IdentityRole類。當我點擊/ API /角色它返回一個空數組如何使用Web API擴展IdentityRole 2 + AspNet標識2
身份模型
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;
}
}
}
出於好奇,你爲什麼要擴展角色類?我發現大多數時候,你真的不需要..你只需要添加項目的索賠。大多數人認爲你必須擴展事物才能獲得更多功能,而這並非總是如此。 – 2014-09-04 21:33:25
我想擴展角色,以便我可以向它們添加一個Permissions對象,並且我想擴展userRoles,以便我可以添加一個虛擬角色對象。現在當我在包含user.Roles的用戶時,它會返回UserRole對象,但是我不能包含UserRole.Role,這樣我就擁有了所有的數據。 – 2014-09-04 21:40:33
爲什麼需要將權限對象添加到角色?角色本質上就是一個標籤。如果你想構建一個複雜的權限系統,我會創建一個將角色映射到權限的UserRole映射對象。我不知道「虛擬角色對象」是什麼意思。我想你會回頭看這個。 – 2014-09-04 21:53:34