2

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?

Azamat
  • 46
  • 1
  • 8
  • You have to login again after adding the role to the user. –  Jul 15 '17 at 10:23
  • I tried, it isn't work, because that binding [User->Role] not saved in database AspNetUserRoles-table (it should be store there). – Azamat Jul 15 '17 at 11:09
  • In that case please show the code where you are trying to save the selected roles. –  Jul 15 '17 at 15:27
  • I'm not saving it with manually, so i'm using the "IdentityManage" for this target, but it is not work (( for your explanation, can you try it by example of this site https://www.scottbrady91.com/ASPNET-Identity/Identity-Manager-using-ASPNET-Identity, it would be great, thanks accordance! – Azamat Jul 17 '17 at 04:19

1 Answers1

0

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);
}
Bharatsing Parmar
  • 2,435
  • 1
  • 9
  • 18
  • yeah, AuthorizeAttribute works fine, but problem is in IdentityManager. It doesn't want to save selected roles (from idm-user-interface) to AspNetUserRoles-table in database. It saved jsust to AspNetClaims-table. Is IdentityManager's bug? – Azamat Jul 15 '17 at 11:06