Friday, September 29, 2017

Can’t find role when adding user to role in Asp.Net API Controller?

So, just when I finally thought I had understood the Microsoft Asp.Net Identity Role System … I run into a new problem…

I have a working controller that does my role management but I had problems in the view so I decided to use angularjs for my Web-Apps Admin-Panel.

Everything seemed to work after a bit of hacking but now I’m at a point that I don’t understand at all. My process:

  • Admin presses a button, which passes a user ID (string) to an angular Controller.
  • Angular makes a POST Request to my ApiController, passing the userID to a Controller action that is supposed to authorize my user.

Here is my API Controller:

public async Task<IHttpActionResult> changeAuth()
    {
        string userID;
        var requestData = HttpContext.Current.Request.InputStream;
        using (StreamReader sr = new StreamReader(requestData))
        {
            userID = sr.ReadToEnd();
        }
        Models.ApplicationUser user;
        try
        {
            user = await UserManager.FindByIdAsync(userID);
            if (user == null)
                throw(new Exception());
        }
        catch(Exception ex)
        {
            return BadRequest("couldn't find user in db");
        }
            foreach(var role in user.Roles)
            {
                testlist.Add(RoleManager.FindById(role.RoleId).Name);
            }
            if (!IsUserInRole(user, authRole))
            {
                IdentityResult res = new IdentityResult();
                try
                {
                    res = await UserManager.AddToRoleAsync(user.Id, authRole.Id);
                }
                catch(Exception ex)
                {
                    return BadRequest(ex);
                }
            }
            if(IsUserInRole(user, authRole)
                return Ok("User is now authorized!");
            else
                return InternalServerError("Adding User to Role failed");
    }

  • I load the userID (which works)
  • I load the user, UserManager that I got from HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
  • I do the same thing with the respective Role (-> authRole), both of these are found and I can see their values in the debugger.
  • User doesn’t have the role yet, so I execute ´await UserManager.AddToRoleAsync(user.Id, authRole.Id);`… and get an exception:

Exception: System.InvalidOperationException: The Role "50ab556b-a8e4-4294-b91c-1f9374c25244" does not exist at Microsoft.AspNet.Identity.EntityFramework.UserStore`6.<AddToRoleAsync>d__3b.MoveNext()

I have no clue why this is happening. Is it the fact that I’m using a WebAPI Controller instead of regular? Maybe because I use HttpContext.Current.GetOwinContext() instead of HttpContext.GetOwinContext() to load the User/Role Manager? But if there’s something wrong with Role-/Usermanager … how does it find the user and the role when I search for it, only when I try to add the user to the role it doesn’t work? AddToRoleAsync is the very same method I used just today, when I registered my TestUsers to add the role “NewUser” to them …

Source: AngularJS



from Angular Questions https://angularquestions.com/2017/09/29/cant-find-role-when-adding-user-to-role-in-asp-net-api-controller/
via @lzomedia #developer #freelance #web #lzomedia.com

No comments:

Post a Comment