Is it possible to save a role with enum parameter

2019-07-30 22:38发布

问题:

I am experimenting with creating a authorization with identity.

So far eveything is working with the guidlines I have found online.

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.

Is this possible?

My code so far:

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
}

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

As I mentioned above I don't wish to have the rolename property, I want to use the enum instead, how is this done?

EDIT: pic of aspNetRoles:

回答1:

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

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


回答2:

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