Asp Net MVC Per Block Permission

2

I am separating the permissions in my application, I am using a custom authorization with the class Authorize, I already can separate permission by action and controller, I would like to know how I use only for snippets of code, for example, on a page I have 2 forms, does each appear depending on the permission?

    
asked by anonymous 31.07.2015 / 15:49

1 answer

2

So, in View :

@if (User.IsInRole("Role1")) 
{
    @* Escreva o form aqui *@
} else if (User.IsInRole("Role2")) 
{
    @* Escreva outro form aqui *@
}

EDIT

I've forgotten an important detail: You must also customize the UserManager to search for Role in a custom way:

App_Start/IdentityConfig.cs

public class ApplicationUserManager : UserManager<ApplicationUser>
{
    ...

    public override async Task<bool> IsInRoleAsync(string userId, string role)
    {
        // Coloque aqui sua regra de negócio para pesquisa de Roles.
        // return await base.IsInRoleAsync(userId, role);
    }

    ...
}
    
31.07.2015 / 15:57