我正在尝试在共享主机上运行WIF依赖方应用程序.他们不会将IIS设置LoadUserProfile设置为true,因此我收到以下错误:
消息:数据保护操作失败.这可能是由于没有为当前线程的用户上下文加载用户配置文件引起的,这可能是线程模拟时的情况.ExceptionStackTrace:在Microsoft.IdentityModel.Web.ProtectedDataCookieTransform.Encode(Byte [] value)的System.Security.Cryptography.ProtectedData.Protect(Byte [] userData,Byte [] optionalEntropy,DataProtectionScope范围)
有没有办法解决?
Eug*_*ace 19
是的,这是因为您使用的是依赖于DPAPI的默认令牌加密.您可以使用基于证书的加密替换它.请参阅此处:http://msdn.microsoft.com/en-us/library/ff803371.aspx(滚动到"对应用程序还有一个更改......")
代码是:
void OnServiceConfigurationCreated(object sender, ServiceConfigurationCreatedEventArgs e)
{
var sessionTransforms =
new List<CookieTransform>(
new CookieTransform[]
{
new DeflateCookieTransform(),
new RsaEncryptionCookieTransform(
e.ServiceConfiguration.ServiceCertificate),
new RsaSignatureCookieTransform(
e.ServiceConfiguration.ServiceCertificate)
});
var readOnlyTransforms = sessionTransforms.AsReadOnly();
var sessionHandler = new SessionSecurityTokenHandler(readOnlyTransforms);
e.ServiceConfiguration.SecurityTokenHandlers.AddOrReplace(sessionHandler);
}
Run Code Online (Sandbox Code Playgroud)
和
void Application_Start(object sender, EventArgs e)
{
FederatedAuthentication.ServiceConfigurationCreated += OnServiceConfigurationCreated;
}
Run Code Online (Sandbox Code Playgroud)
两者都在global.asax.cs上
顺便说一句,这也是"web farm友好"配置WIF的方式,因此它是机器(实例)独立的.Windows Azure部署本质上是Web场,因此您可以在该章中看到它.
更新:在较新的版本中,API已更改.更新的代码看起来像这样
void OnFederationConfigurationCreated(object sender, FederationConfigurationCreatedEventArgs e)
{
var sessionTransforms = new List<CookieTransform>(
new CookieTransform[]
{
new DeflateCookieTransform(),
new RsaEncryptionCookieTransform(e.FederationConfiguration.ServiceCertificate),
new RsaSignatureCookieTransform(e.FederationConfiguration.ServiceCertificate)
});
var sessionHandler = new SessionSecurityTokenHandler(sessionTransforms.AsReadOnly());
e.FederationConfiguration
.IdentityConfiguration
.SecurityTokenHandlers
.AddOrReplace(sessionHandler);
}
Run Code Online (Sandbox Code Playgroud)
和
protected void Application_Start()
{
FederatedAuthentication.FederationConfigurationCreated += OnFederationConfigurationCreated;
}
Run Code Online (Sandbox Code Playgroud)
您还可以使用命名空间中.net 4.5中提供的MachineKeySessionSecurityTokenHandlerSystem.IdentityModel.Services.Tokens.您可以通过在配置中进行设置来启用此令牌处理程序.
<system.identityModel>
<identityConfiguration>
<securityTokenHandlers>
<remove type="System.IdentityModel.Tokens.SessionSecurityTokenHandler,
System.IdentityModel, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=B77A5C561934E089" />
<add type="System.IdentityModel.Services.Tokens.MachineKeySessionSecurityTokenHandler,
System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=B77A5C561934E089">
</add>
</securityTokenHandlers>
</identityConfiguration>
</system.identityModel>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3782 次 |
| 最近记录: |