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: