简体   繁体   中英

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

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?

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM