AspNet Identity Roles - Custom

0

My application has user groups that are customizable, that is, the end user creates and gives access to certain permissions.

For this, I have my records

Module, Menu, Item, Option

Module = Financial, Registration, etc. Menu = Accounts, Financial Setup, etc. Item = Customer, Accounts Payable, etc. Option = New, Change, Delete, Preview

In this project, I used ASP.NET MVC4 and Forms Authentication. And to filter define these custom "ROLES", I have my filter customizable in my controllers.

I do not know if it was clear.

Now I have a new ASP.NET MVC5 project and I will use ASP.NET Identity for authorization and authentication of my users.

I would like to know if the new Asp.net Identity Roles, I have these customizable Roles, or I will also have to do this filter again.

Because in the examples I've seen so far, the only way to check the role, would be to Authorize (Role="Name Role")

I looked for other examples of role, also the same form.

    
asked by anonymous 17.06.2014 / 21:43

1 answer

0

The way to extend Roles is different in ASP.NET Identity. Use Controllers + a class IdentityManager :

public class IdentityManager
{
    RoleManager<ApplicationRole> _roleManager = new RoleManager<ApplicationRole>(
        new RoleStore<ApplicationRole>(new ApplicationDbContext()));

    UserManager<ApplicationUser> _userManager = new UserManager<ApplicationUser>(
        new UserStore<ApplicationUser>(new ApplicationDbContext()));

    ApplicationDbContext _db = new ApplicationDbContext();


    public bool RoleExists(string name)
    {
        return _roleManager.RoleExists(name);
    }


    public bool CreateRole(string name, string description = "")
    {
        var idResult = _roleManager.Create(new ApplicationRole(name, description));
        return idResult.Succeeded;
    }


    public bool CreateUser(ApplicationUser user, string password)
    {
        var idResult = _userManager.Create(user, password);
        return idResult.Succeeded;
    }


    public bool AddUserToRole(string userId, string roleName)
    {
        var idResult = _userManager.AddToRole(userId, roleName);
        return idResult.Succeeded;
    }


    public void ClearUserRoles(string userId)
    {
        var user = _userManager.FindById(userId);
        var currentRoles = new List<IdentityUserRole>();

        currentRoles.AddRange(user.Roles);
        foreach (var role in currentRoles)
        {
            _userManager.RemoveFromRole(userId, role.Role.Name);
        }
    }
}

The class to be extended is IdentityRole , and the class that manages Roles is RoleManager :

public class ApplicationRole : IdentityRole
{
    public ApplicationRole() : base() { }
    public ApplicationRole(string name, string description) : base(name)
    {
        this.Description = description;
    }
    public virtual string Description { get; set; }
}

Creating a Role looks like this:

var idManager = new IdentityManager();

success = idManager.CreateRole("Admin", "Global Access");

The example implementation is quite extensive, and can be found here: link

More information here: link

    
18.06.2014 / 04:36