1

I'm in a project where only administrators can add new members to the system. But I'm getting the following error:

The data protection operation was unsuccessful. This may have been caused by not loading the user profile into the user context of the current thread, which can happen when the thread is impersonating.

Description: An unhandled exception occurred during the execution of the current WEB request. Examine the stack trace for more information about the error and where it originated in the code.

Exception Details: System. Security. Cryptography. Cryptographicexception: The data protection operation was unsuccessful. This may have been caused by not loading the user profile into the user context of the current thread, which can happen when the thread is impersonating.

The problem only happens after the project is published.

My Controller

[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);
}
Community
  • 1
  • 1

1 Answers1

0

I managed to get around the problem by commenting on the following code:

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