简体   繁体   English

在Azure上使用Autofac时数据保护操作失败

[英]Data protection operation unsuccessful when using Autofac on Azure

I am getting this error when hosting my application on azure: 在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/ 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: 说您应该在启动类上创建一个实例,但是我使用的是autofac,所以我这样做:

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

and then in my UserManager constructor, I do this: 然后在我的UserManager构造函数中,执行以下操作:

// 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? 有谁知道我该如何解决这个问题?

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: 您可以使用IAppBuilder.GetDataProtectionProvider()代替声明新的DpapiDataProtectionProvider ,根据上述代码,您需要进行如下修改:

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: 这是Startup.Auth.cs的代码片段,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM