Asp Net MVC Block Permission

Asked

Viewed 96 times

2

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

1 answer

2


Thus, in the View:

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

EDIT

I forgot an important detail: It is necessary to customize the UserManager to get the Roll in a customized 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);
    }

    ...
}
  • That one User already gets the logged in user, or I have to pass it to view?

  • 1

    Already picks up the logged in user. It is automatically filled in by Controller.

  • I created a custom authorization [MinhaAuth("TL001","TL002")] and I’m trying to @if (User.IsInRole("TL002")){} but it’s not going, there’s something else to do?

  • @Henriquedomingospereira I will have to expand the answer to customize the RoleManager. I’m just looking for references and already complete the answer.

  • @Henriquedomingospereira RoleManager nay. UserManager. I edited the answer.

Browser other questions tagged

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