Search code examples
c#asp.netasp.net-mvcasp.net-web-apiasp.net-identity

Asp.net Roles Web Api check annotation "authorize" programmatically


I need to check if the current logged user can use a controller method BEFORE calling it. es: I am logged as USER (group:users) and i want to check if i am authorized to call "delete()" method with annotation: "[Authorize(Roles='Users')]"

There is a way to solve this? thanks in advance.


Solution

  • You can use the UserManager to check if the user is in a given role.

    ApplicationUser user = await UserManager.FindByEmailAsync(model.Email);
    if (UserManager.IsInRole(user.Id, "Users"))
    {
        // Do something.
    }