MVC5: UserManager.AddToRole(): “Error Adding User to Role: UserId not found”?

前端 未结 6 1808
名媛妹妹
名媛妹妹 2020-12-25 13:33

I have been experimenting with MVC5/EF6 and trying out the new Identity Authentication with Code-First Migrations. Everything in the solution is currently building and I can

6条回答
  •  南笙
    南笙 (楼主)
    2020-12-25 13:44

    the issue in my case is that i use (Username) of user instead of Id, working code below:

     public async Task Register(RegisterViewModel model)
            {
                if (ModelState.IsValid)
                {
                    var user = new ApplicationUser { UserName = model.Username, Email = model.Username, workshopId =model.workshopId};
                    var rolesList = _context.Roles.ToList();
                    string role = rolesList.FirstOrDefault(r => r.Id == model.SelectedRoleId).Name;
                    var result = await UserManager.CreateAsync(user, model.Password);
    
                    // Add User to the selected role from the list in .cshtml
                    if (result.Succeeded)
                    {
                        await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
                        result =await UserManager.AddToRoleAsync(user.Id, role);
                        // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
                        // Send an email with this link
                        // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                        // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                        // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking here");
    
                        return RedirectToAction("Index", "Home");
                    }
                    AddErrors(result);
                }
    
                // If we got this far, something failed, redisplay form
                return View(model);
            }
    

提交回复
热议问题