我在一个项目中,只有管理员可以将新成员添加到系统中。但出现以下错误:
数据保护操作失败。这可能是由于未将用户配置文件加载到当前线程的用户上下文中引起的,这种情况可能是在模拟线程时发生的。
描述:在执行当前WEB请求期间发生未处理的异常。检查堆栈跟踪,以获取有关错误及其在代码中起源的更多信息。
异常详细信息:系统。安全。密码学。加密异常:数据保护操作未成功。这可能是由于未将用户配置文件加载到当前线程的用户上下文中引起的,这种情况可能是在模拟线程时发生的。
该问题仅在项目发布后发生。
我的控制器
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model, HttpPostedFileBase AvatarAux = null)
{
ClaimsIdentity identity = (ClaimsIdentity)User.Identity;
string idRepresentanteLogado = identity.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Sid).Value;
var user = new ApplicationUser
{
UserName = model.Email,
Email = model.Email,
IsInativo = model.IsInativo,
DataCadastro = model.DataCadastro,
Nome = model.Nome,
Id_Tipo_Representante = model.Tipo_Representante.Id,
UserResponsavel = _userManager.FindById(idRepresentanteLogado),
Avatar = AvatarAux != null ? ImagemConverter.ImagemToByte(AvatarAux) : ImagemConverter.ObterImagemUsuarioDefault(),
};
var result = await _userManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
var code = await _userManager.GenerateEmailConfirmationTokenAsync(user.Id);
var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code }, protocol: Request.Url.Scheme);
await _userManager.SendEmailAsync(user.Id, "Confirme sua Conta", "Por favor confirme sua conta clicando neste link: <a href='" + callbackUrl + "'></a>");
ViewBag.Link = callbackUrl;
return View("DisplayEmail");
}
AddErrors(result);
return View(model);
}
答案 0 :(得分:0)
我设法通过注释以下代码来解决此问题:
if (result.Succeeded)
{
//var code = await _userManager.GenerateEmailConfirmationTokenAsync(user.Id);
//var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code }, protocol: Request.Url.Scheme);
//await _userManager.SendEmailAsync(user.Id, "Confirme sua Conta", "Por favor confirme sua conta clicando neste link: <a href='" + callbackUrl + "'></a>");
//ViewBag.Link = callbackUrl;
return RedirectToAction("ListUsers", "Account");
}