Aspnet Identity Roles - Custom

Asked

Viewed 762 times

0

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

For that, I have my records

Module, Menu, Item, Option

Module = Financial, Registration, etc Menu = Accounts, Financial settings, etc Item = Customer, Accounts Payable, etc Option = New, Change, Delete, View

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

I don’t know if it’s clear.

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

I wonder if in the new Asp.net Identity Roles, I have these customizable roles, or I will also have to make this filter again.

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

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

1 answer

0


The way to extend Roles is different in ASP.NET Identity. It is used 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 now is the IdentityRole, and the class that manages Roles is the 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; }
}

The creation of a Roll gets like this:

var idManager = new IdentityManager();

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

The example implementation is quite extensive, and can be obtained here: https://github.com/TypecastException/AspNetExtendingIdentityRoles

More information here: http://www.codeproject.com/Articles/727054/ASP-NET-MVC-Identity-Extending-and-Modifying-R

  • I saw this link, but there is the doubt, I still need to pass in my actions [Authorize(Roles="My Role")] ? Because this is customizable by the customer, I cannot pass fixed parameters, in the old Forms Authentication I used a customizable filter, and now in Identity?

  • 1

    It’s the same scheme: you drift the AuthorizeAttribute and puts all logic within the method AuthorizeCore.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.