是否可以使用Identity使用enum参数保存角色?

时间:2016-07-27 12:19:19

标签: c# asp.net-mvc-5 asp.net-identity

我正在尝试使用身份创建授权。

到目前为止,eveything正在使用我在网上找到的指导。

但是我想修改角色的保存。我创建了一个枚举,我将这个枚举保存到aspNetRoles表中(代码如下)。

将角色保存到用户的方式如下:

UserManager.AddToRole(user.Id, "Admin");

我不喜欢作为第二个参数发送字符串并希望发送枚举。

这可能吗?

到目前为止我的代码:

通过添加自定义属性修改aspnetRoles表:

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中添加角色:

    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属性,我想使用枚举,这是怎么做的?

编辑: asp.netRoles的照片:

enter image description here

2 个答案:

答案 0 :(得分:1)

public enum Roles
{
  Developer,
  Manager
 }

if (!roleManager.RoleExists(Roles.Developer))
{
    // first we create Admin rool   
    var role = new ApplicationRole();
    role.Name = Roles.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, Roles.developer);
    }
}

答案 1 :(得分:1)

UserManager.AddToRoleextension method,因此您可以编写类似的方法将您的用户添加到您的角色中:

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",该怎么办?字符串常量重命名很难操作,可能存在错误。

您可以做的是为您的角色声明命名常量:

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);