gpt4 book ai didi

c# - 使用 autofac 在 azure 上进行数据保护操作失败

转载 作者:行者123 更新时间:2023-12-03 03:03:50 25 4
gpt4 key购买 nike

我在尝试创建或更新用户时收到此错误。完整的错误是:

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.

我们在应用程序中使用 autofac,所以我 after reading this article我创建了自己的 IdentityFactoryOptions,如下所示:

public class IdentityFactoryOptions: IdentityFactoryOptions<UserProvider>
{
public IdentityFactoryOptions()
{
DataProtectionProvider = new DpapiDataProtectionProvider("ASP.NET Identity");
}
}

然后我创建了自己的 DataProtectionTokenProvider,如下所示:

public class DataProtectionTokenProvider : DataProtectorTokenProvider<User>
{
public DataProtectionTokenProvider(IdentityFactoryOptions options) : base(options.DataProtectionProvider.Create("ASP.NET Identity"))
{
TokenLifespan = TimeSpan.FromHours(6);
}
}

我将它们注册为SingleInstances,如下所示:

builder.RegisterType<IdentityFactoryOptions>().AsSelf().SingleInstance();
builder.RegisterType<DataProtectionTokenProvider>().AsSelf().SingleInstance();

我将DataProtectionTokenProvider注入(inject)到我的UserManager中,并在管理器构造函数中将其分配如下:

UserTokenProvider = dataProtectionTokenProvider;

但是完成所有这些之后,我仍然收到错误。我也read this article看到你也必须更新你的 web.config,所以我添加了这个:

<system.identityModel>
<identityConfiguration>
<securityTokenHandlers>
<add type="System.IdentityModel.Services.Tokens.MachineKeySessionSecurityTokenHandler, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<remove type="System.IdentityModel.Tokens.SessionSecurityTokenHandler, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</securityTokenHandlers>
</identityConfiguration>
</system.identityModel>

但错误仍然存​​在。有人对此有任何解决方案吗?这让我发疯......

最佳答案

好的,我设法解决了这个问题。使用我链接的第一篇文章,我将 IAppBuilder 注入(inject)到我的 autofac 模块中,并删除了我的 IdentityFactoryOptions 类。所以现在的注册看起来像这样:

builder.Register(m => new DataProtectorTokenProvider(_app.GetDataProtectionProvider())).AsSelf().SingleInstance();

DataProtectorTokenProvider 如下所示:

public class DataProtectorTokenProvider : DataProtectorTokenProvider<User>
{
public DataProtectorTokenProvider(IDataProtectionProvider dataProtectionProvider) : base(dataProtectionProvider.Create("ASP.NET Identity"))
{
TokenLifespan = TimeSpan.FromHours(6);
}
}

其他一切我都保持不变。这解决了问题。

关于c# - 使用 autofac 在 azure 上进行数据保护操作失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46319940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com