Error generating E-mail Confirmation Token in Asp.MVC

0

I made an application that uses the Asp.Net MVC login system. I tried locally sending the token for email confirmation and it works perfectly, but when I went to Azure in WebApp in the free account for testing purposes I always get this error when I 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 noticed that the error always occurs in this line that is in the AccountController class, Register method:

var code = await _userManager.GenerateEmailConfirmationTokenAsync(user.Id);
    
asked by anonymous 31.05.2016 / 14:49

1 answer

0

The problem is this: You are now developing for cloud, so you can not create a data provider for each request, but keep only one per application.

Check out this answer in English .

To work around this, you should 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, you should always use the same 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
    }
}

In both login token generation and tokens for password swapping and recovery.

    
01.06.2016 / 10:07