Error generating Email Confirmation Token in Asp.MVC

Asked

Viewed 98 times

0

I made an application that uses the Asp.Net MVC login system. I have tested locally the sending of the token for e-mail confirmation and it works perfectly, but when I went up to Azure in the Webapp in the free account for test afim always I get this error when I will register a new user.

The data Protection Operation was unsuccessful. This may have been caused by not having the user profile Loaded for the Current thread’s user context, which may be the case when the thread is impersonating.

The user is registered but failed to send or generate the same, I realized that the error always occurs in this line that is in the Accountcontroller class, Register method:

var code = await _userManager.GenerateEmailConfirmationTokenAsync(user.Id);
  • 1

    From the context of the message, you are trying to send an email to a user who "does not exist" (because it is not yet logged in, hence the system does not know who to send it to).

  • @Uilquemessias, pointed out the error well, but to remove all doubt post the code please Robz so that we have possibilities to point out the problems.

1 answer

0


The problem is this: Vc is now developing for cloud, so you cannot create a data provider at each request, but keep only one per application.

See here this answer in English.

To get around this, you must create a single Dataprovider at the start of your application.

public partial class Startup
{
    internal static IDataProtectionProvider DataProtectionProvider { get; private set; }

    public void ConfigureAuth(IAppBuilder app)
    {
        DataProtectionProvider = app.GetDataProtectionProvider();
        // other stuff.
    }
}

From there, the same should always be used Dataprovider at all times.

public class UserManager : UserManager<ApplicationUser>
{
    public UserManager() : base(new UserStore<ApplicationUser>(new MyDbContext()))
    {
        var dataProtectionProvider = Startup.DataProtectionProvider;
        this.UserTokenProvider = 
                new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));

        // do other configuration
    }
}

Both in the generation of login tokens, as in the Tokes for exchanging and recovering passwords.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.