locked
how to CREATE an identity user and then assign a role? User not found RRS feed

  • Question

  • User2142845853 posted

    This problem is about combining 2 steps, how to finish one fully then do a 2nd

    A c# standard MVC Entity framework project where the CREATE method tries to create a new user in Identity, THEN tries to assign a role to the newly created user.  It fails because the reference to add a role happens too fast, it cant keep up, exception is 'user not found' but the 2nd time around it works.  

     try
                {
                    var user3 = new ApplicationUser { UserName = usrid, Email = usremail };
                    var result = UserManager.Create(user3, usrdat);
                   
    
                    if (result.Succeeded)
                    {
                        TempData["Message"] = " created New user successfully";
                    }
                    else
                    {
                        TempData["ErrorMessage"] = "Failed to create new user: " + result.Errors.FirstOrDefault().ToString();
                        return View(UserList);
                    }
                   
    
                }
                catch (Exception eet)
                {
                    TempData["ErrorMessage"] = "Exception thrown creating New User: " + eet.Message.ToString();
                    return View(UserList);
                }
                finally
                {
    
                }
    
                try
                {
                    var user4 = new ApplicationUser { UserName = usrid, Email = usremail };
                    var roleresult = UserManager.AddToRole(user4.Id, usrrole);
                    if (roleresult.Succeeded)
                    {
                        TempData["Message"] = " Added Permission to User successfully";
                    }
                    else
                    {
                        TempData["ErrorMessage"] = "Failed to add Permission to new User: " + roleresult.Errors.FirstOrDefault().ToString();
                        return View(UserList);
                    }
                }
                catch(Exception roleadd)
                {
                    TempData["ErrorMessage"] = "Exception thrown assigning Permission New User: " + roleadd.Message.ToString();
                    return View(UserList);
                }
    
    

    It fails with the roleadd exception: user does not exist (or user not found).  I added the Finally() to the first TRY, which always runs fine, it takes the passed in params for the HTTP POST Create, just gets various values.  In the 2nd failing try/catch I try casting a new user4 as a new reference to the Identity.  The solution would be:

     try
                {
                    var user4 = new ApplicationUser { UserName = usrid, Email = usremail };
                    var roleresult = UserManager.AddToRole(user4.Id, usrrole);
               WAIT UNTIL user4.UserName.Exists(2000);  //waits 2 seconds for the operation or throws an exception.

    New feature 'WAIT UNTIL' would be useful.  Aside from code that doesnt exist, how could this timing conflict be resolved?

    Wednesday, June 12, 2019 7:40 PM

All replies

  • User475983607 posted

    This problem is about combining 2 steps, how to finish one fully then do a 2nd

    A c# standard MVC Entity framework project where the CREATE method tries to create a new user in Identity, THEN tries to assign a role to the newly created user.  It fails because the reference to add a role happens too fast, it cant keep up, exception is 'user not found' but the 2nd time around it works.  

     try
                {
                    var user3 = new ApplicationUser { UserName = usrid, Email = usremail };
                    var result = UserManager.Create(user3, usrdat);
                   
    
                    if (result.Succeeded)
                    {
                        TempData["Message"] = " created New user successfully";
                    }
                    else
                    {
                        TempData["ErrorMessage"] = "Failed to create new user: " + result.Errors.FirstOrDefault().ToString();
                        return View(UserList);
                    }
                   
    
                }
                catch (Exception eet)
                {
                    TempData["ErrorMessage"] = "Exception thrown creating New User: " + eet.Message.ToString();
                    return View(UserList);
                }
                finally
                {
    
                }
    
                try
                {
                    var user4 = new ApplicationUser { UserName = usrid, Email = usremail };
                    var roleresult = UserManager.AddToRole(user4.Id, usrrole);
                    if (roleresult.Succeeded)
                    {
                        TempData["Message"] = " Added Permission to User successfully";
                    }
                    else
                    {
                        TempData["ErrorMessage"] = "Failed to add Permission to new User: " + roleresult.Errors.FirstOrDefault().ToString();
                        return View(UserList);
                    }
                }
                catch(Exception roleadd)
                {
                    TempData["ErrorMessage"] = "Exception thrown assigning Permission New User: " + roleadd.Message.ToString();
                    return View(UserList);
                }
    

    It fails with the roleadd exception: user does not exist (or user not found).  I added the Finally() to the first TRY, which always runs fine, it takes the passed in params for the HTTP POST Create, just gets various values.  In the 2nd failing try/catch I try casting a new user4 as a new reference to the Identity.  The solution would be:

     try
                {
                    var user4 = new ApplicationUser { UserName = usrid, Email = usremail };
                    var roleresult = UserManager.AddToRole(user4.Id, usrrole);
               WAIT UNTIL user4.UserName.Exists(2000);  //waits 2 seconds for the operation or throws an exception.

    New feature 'WAIT UNTIL' would be useful.  Aside from code that doesnt exist, how could this timing conflict be resolved?

    It's not a timing issue.  It a bug in your code.  First you newed an ApplicationUser You saved user3 which populates the user3 instance with a new Id.  Then you created a new ApplicationUser instance which does not have an Id.

    Here's an example of creating a new user

            public ActionResult CreateUser(string Username, string Email)
            {
                ApplicationUser user = new ApplicationUser() { UserName = Username, Email = Email };
                var result = UserManager.Create(user, "@Password123");
                if (!result.Succeeded)
                {
                    return Json(result.Errors.FirstOrDefault(), JsonRequestBehavior.AllowGet);
                }
    
                result = UserManager.AddToRole(user.Id, "User");
                if (!result.Succeeded)
                {
                    return Json(result.Errors.FirstOrDefault(), JsonRequestBehavior.AllowGet);
                }
    
                var roles = UserManager.GetRolesAsync(user.Id);
    
                return Json(new { user = user, roles = roles}, JsonRequestBehavior.AllowGet);
            }

    Wednesday, June 12, 2019 9:04 PM
  • User2142845853 posted

    Well I started out with one instance, one ApplicationUser and it didnt work.  I moved the new application user outside of the try thought i did that before but it works.

    Why do you use the JSON there like that?

    Wednesday, June 12, 2019 10:27 PM
  • User475983607 posted

    Well I started out with one instance, one ApplicationUser and it didnt work.  I moved the new application user outside of the try thought i did that before but it works.

    It's hard to comment without the source code that causes the issue.

    Why do you use the JSON there like that?

    Laziness, it's less work than rendering a View and passing a model or ViewBag when building demo code.

    Wednesday, June 12, 2019 11:36 PM