我已经使用Int PK自定义了Asp.Net Identity,因此我创建了以int为键类型的自定义类。代码基于http://bitoftech.net/2015/01/21/asp-net-identity-2-with-asp-net-web-api-2-accounts-management/,由Roles and Claims Auth编写,非常有效。
public class UserRoleIntPk : IdentityUserRole<int>
{
}
public class RoleIntPk : IdentityRole<int, UserRoleIntPk>
{
}
public class RoleStoreIntPk : RoleStore<RoleIntPk, int, UserRoleIntPk>
{
public RoleStoreIntPk(ApplicationDbContext context)
: base(context)
{
}
}
public class UserStoreIntPk : UserStore<ApplicationUser,RoleIntPk,int,UserLoginIntPk,UserRoleIntPk,UserClaimIntPk>
{
public UserStoreIntPk(ApplicationDbContext context)
: base(context)
{
}
}
public class UserLoginIntPk : IdentityUserLogin<int>
{
}
public class UserClaimIntPk : IdentityUserClaim<int>
{
}
public class AuthManagers : UserManager<ApplicationUser,int>
{
public AuthManagers(IUserRoleStore<ApplicationUser, int> userStore )
: base(userStore)
{
}
public static AuthManagers Create(IdentityFactoryOptions<AuthManagers> options, IOwinContext context)
{
var appDbContext = context.Get<ApplicationDbContext>();
var appUserManager = new AuthManagers(new UserStoreIntPk(appDbContext));
//Rest of code is removed for clarity
appUserManager.EmailService = new EmailService();
var dataProtectionProvider = options.DataProtectionProvider;
if (dataProtectionProvider != null)
{
appUserManager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser,int>(dataProtectionProvider.Create("ASP.NET Identity"))
{
//Code for email confirmation and reset password life time
TokenLifespan = TimeSpan.FromHours(6)
};
}
appUserManager.UserValidator = new UserValidator<ApplicationUser,int>(appUserManager)
{
AllowOnlyAlphanumericUserNames = true,
RequireUniqueEmail = true,
};
//Configure validation logic for passwords
appUserManager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = true,
RequireDigit = true,
RequireLowercase = false,
RequireUppercase = false,
};
return appUserManager;
}
}
public class ApplicationRoleManager : RoleManager<RoleIntPk, int>
{
public ApplicationRoleManager(IRoleStore<RoleIntPk, int> roleStore)
: base(roleStore)
{
}
public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)
{
var appRoleManager = new ApplicationRoleManager(new RoleStoreIntPk(context.Get<ApplicationDbContext>()));
return appRoleManager;
}
}
我是否缺少某些内容或我缺少的任何基本实现?我可以只使用Entities并为此创建一个自定义存储过程,但是由于users属性已经存在,所以我认为这会太过分了。在我的两个班上,我有自定义身份模型和管理器
问题出在RoleController的路线“ GetAllRoles”中
var roles = AppRoleManager.Roles;
会产生以下JSON输出:
[
{
"users": [],
"id": 2,
"name": "Admin"
},
{
"users": [],
"id": 1,
"name": "SuperAdmin"
},
{
"users": [],
"id": 3,
"name": "User"
}]
问题在于,当应该在指定角色中包含用户ID时,users属性为空。