UserManager.Create 函数调用后的问题

problems after UserManager.Create function call

我在调用 UserManager.Create 方法后遇到了一些问题。问题是当前登录的用户在创建新用户后丢失了他的凭据。我会把我的代码放在这里

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);
        }

删除发送确认邮件的代码我没有问题。有谁知道什么会影响那里?

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>");

问题似乎是您正在尝试对 user 对象执行 play。您应该获得对已创建用户的 true 引用。

检查用户是否创建成功后,可以这样做:

if(result.succeeded)
{
  var theNewUser = await UserManager.FindByEmailAsync(model.Email);
//Do other things with the new user.
}

我在发送电子邮件的功能中收到一个错误,这个错误似乎影响了会话,用户被重定向到登录页面。而且它发生在开发模式中,因为电子邮件问题只出现在生产环境中。感谢 bolkay 的帮助。