是否可以使用Identity保存带有枚举参数的角色? [英] Is it possible to save a role with enum parameter using Identity?
问题描述
我正在尝试创建具有身份的授权.
I am experimenting with creating a authorization with identity.
到目前为止,一切工作都与我在网上找到的指导方针相配合.
So far eveything is working with the guidlines I have found online.
但是我想修改角色的保存.我已经创建了一个枚举,并将该枚举保存到aspNetRoles表中(代码如下).
However I want to modify the saving of roles. I have created an enum and I save this enum to aspNetRoles table (code will follow below).
将角色保存到用户的方式是通过以下方式:
The way a role is saved to a user is via the following:
UserManager.AddToRole(user.Id, "Admin");
我不喜欢发送字符串作为第二个参数,而是希望发送一个枚举.
I am not a fan of sending in a string as a second paramater and wish to send an enum instead.
这可能吗?
到目前为止,我的代码:
My code so far:
通过添加自定义属性来修改aspnetRoles表:
modification to aspnetRoles table by adding a custom property:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
new public DbSet<ApplicationRole> Roles { get; set; }
}
public class ApplicationRole : IdentityRole
{
[Required]
public RoleId roleId { get; set; }
}
public enum RoleId
{
Developer = 0,
Administrator = 1,
Other = 2
}
在startup.cs中添加角色:
adding a role in startup.cs:
if (!roleManager.RoleExists("Developer"))
{
// first we create Admin rool
var role = new ApplicationRole();
role.Name = "Developer";
role.roleId = RoleId.Developer;
roleManager.Create(role);
//Here we create a Admin super user who will maintain the website
var user = new ApplicationUser();
user.UserName = "ra3iden";
user.Email = "Email@gmail.com";
user.FirstName = "Myname";
user.LastName = "LastName";
string userPWD = "A@Z200711";
var chkUser = UserManager.Create(user, userPWD);
//Add default User to Role Admin
if (chkUser.Succeeded)
{
var result1 = UserManager.AddToRole(user.Id, "Developer");
}
}
如上所述,我不希望拥有rolename属性,我想使用枚举,这是怎么做的?
As I mentioned above I don't wish to have the rolename property, I want to use the enum instead, how is this done?
aspNetRoles图片:
pic of aspNetRoles:
推荐答案
UserManager.AddToRole
is an extension method, so you can write similar method to add your users to your roles:
public static IdentityResult AddToRole<TUser, TKey>(
this UserManager<TUser, TKey> manager,
TKey userId,
RoleId role
)
where TUser : class, Object, IUser<TKey>
where TKey : Object, IEquatable<TKey>
{
return manager.AddToRoleAsync(userId, role.ToString())
.Result;
}
但也许这种情况需要采取不同的方法. "Admin"
不是魔术常数,而是一个常数.如果您想将"Admin"
重命名为"Administrator"
怎么办?字符串常量重命名操作很困难,可能会出现错误.
But maybe this situation requires a different approach. "Admin"
is not a magic constant, but it's a constant. What if you'll want rename "Admin"
to "Administrator"
? String constant renaming is difficult operation with possible errors.
您可以做的是为您的角色声明命名常量:
What you can do is to declare named constant for your roles:
public static class Role
{
public const string Admin = "Admin";
public const string Developer = "Developer";
public const string Other = "Other";
}
. . .
UserManager.AddToRole(user.Id, Role.Admin);
这篇关于是否可以使用Identity保存带有枚举参数的角色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!