asp.net identitymanager doesn't work, he doesn't bind user with role in db (table: AspNetUserRoles)

114 views Asked by At

i'm trying to use IdentityManager (just first), and it looks very greatful, and it is creating the role (it's cool), but why it isn't bind the user with the selected role (int idm), because if when i use the attribute [Authorize(Roles = "Admin")] in Home/Contact (for example) it doesn't work. It doesn't to save the selected roles (from user-interface) to AspNetUserRoles-table in database. It just saved to AspNetClaims-table. Is IdentityManager's bug?

1

There are 1 answers

1
Bharatsing Parmar On

You can try AuthorizeAttribute and set custom error message in it.

public class WebApiAuthorizeAttribute : AuthorizeAttribute
{
    protected override void HandleUnauthorizedRequest(HttpActionContext actionContext)
    {
        if (!actionContext.RequestContext.Principal.Identity.IsAuthenticated)
        {
            //Logic for when api not authenticated
        }
        base.HandleUnauthorizedRequest(actionContext);
    }
}

Controller api method in which above authorize attribute used.

[HttpGet]
[WebApiAuthorizeAttribute(Roles="Admin")]      
public async Task<HttpResponseMessage> TestMethod()
{
    return Request.CreateResponse(HttpStatusCode.OK);
}