Search code examples
c#asp.net-mvcasp.net-identityowin

problems after UserManager.Create function call


I'm having some problem after calling the UserManager.Create method. The thing is that the current logged user lost his credentials after create a new user. I'll put my code here

public async Task<ActionResult> CrearUser(CreateUserModel model)
     {
       if (ModelState.IsValid)
         {
                var user = new ApplicationUser
                {
                    FirstName = model.FirstName,
                    LastName = model.LastName,
                    UserName = model.UserName,
                    Email = model.Email,
                    PhoneNumber = model.PhoneNumber,
                    ActiveSince = Convert.ToDateTime(model.ActiveSince),
                    ActiveUntil = Convert.ToDateTime(model.ActiveUntil)
                };
                var result =  UserManager.Create(user, model.Password);
                if (result.Succeeded)
                {


                    // 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 = HttpUtility.UrlEncode(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 <a href=\"" + callbackUrl + "\">here</a>");


                    var roleStore = new RoleStore<IdentityRole>();
                    var roleMngr = new RoleManager<IdentityRole>(roleStore);
                    string name = roleMngr.FindById(model.rolid).Name;
                    await this.UserManager.AddToRoleAsync(user.Id, name);

                    Success(string.Format(" Usuario <b>{0}</b> Creado con Exito.", model.UserName), true);
                    return RedirectToAction("Index");
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            model.Roles = GetRoles();
            return View(model);
        }

removing the code to send the confirmation mail I do not have the problem. Does anyone know what could be influencing there?

string code = HttpUtility.UrlEncode(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 <a href=\"" + callbackUrl + "\">here</a>");

Solution

  • I received an error in the function to send an email and it seems that this error was affecting the session and the user was redirected to the login page. And it was happening in the development mode because the email problem was only in the production environment. Thanks bolkay for the help.