1

I am getting this error when hosting my application on azure:

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.

When trying creating a user or resending a confirmation email. This article:

http://tech.trailmax.info/2014/06/asp-net-identity-and-cryptographicexception-when-running-your-site-on-microsoft-azure-web-sites/

says that you should create a single instance on the startup class, but I am using autofac, so I have done this:

builder.Register(c => new IdentityFactoryOptions<UserProvider>() { DataProtectionProvider = new DpapiDataProtectionProvider(c.Resolve<PiiiCKConfig>().Issuer) }).SingleInstance();

and then in my UserManager constructor, I do this:

// Get our data protection provider
var dataProtectionProvider = options.DataProtectionProvider;

// If we have on
if (dataProtectionProvider != null)
{

    // Set our token provider
    UserTokenProvider = new DataProtectorTokenProvider<User>(dataProtectionProvider.Create("PiiiK Identity"))
    {

        // Set our long the email confirmation token will last
        TokenLifespan = TimeSpan.FromHours(6)
    };
}

But I still get the error. Does anyone know how I can solve this issue?

r3plica
  • 13,017
  • 23
  • 128
  • 290

1 Answers1

2

According to your description, I checked this issue on my side and I could reproduce this issue. Per my test, you could follow the approaches below to solve this issue:

builder.Register(c => new IdentityFactoryOptions<UserProvider>() { DataProtectionProvider = new DpapiDataProtectionProvider(c.Resolve<PiiiCKConfig>().Issuer) }).SingleInstance();

You could using IAppBuilder.GetDataProtectionProvider() instead of declaring a new DpapiDataProtectionProvider, based on the above code, you need to modify it as follows:

builder.Register(c => new IdentityFactoryOptions<UserProvider>()
{
    DataProtectionProvider = app.GetDataProtectionProvider()
}).SingleInstance();

Or

builder.Register<IDataProtectionProvider>(c => app.GetDataProtectionProvider()).SingleInstance();

Additionally, here is a similar issue, you could refer to here.

UPDATE:

Here is the code snippet of Startup.Auth.cs as follows:

public partial class Startup
{
    public void ConfigureAuth(IAppBuilder app)
    {
        var builder = new ContainerBuilder();
        builder.RegisterControllers(typeof(MvcApplication).Assembly);

        builder.Register(c => new IdentityFactoryOptions<ApplicationUserManager>()
        {
            DataProtectionProvider = app.GetDataProtectionProvider()
        }).SingleInstance();

        //Or
        //builder.Register<IDataProtectionProvider>(c =>app.GetDataProtectionProvider()).SingleInstance();

        var container = builder.Build();
        DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
    }
}
Bruce Chen
  • 18,207
  • 2
  • 21
  • 35
  • How do I get IAppBuilder into my overridden load method? – r3plica Jul 18 '17 at 08:27
  • 1
    I did it slightly differently. I have a `Module` set up, so I just created a public property for the `IAppBuilder` interface and created it like this: `builder.RegisterModule(new AutofacModule() { App = app });` and that worked. I shall mark your answer as correct :) – r3plica Jul 18 '17 at 08:51