Ros*_*gan 27 asp.net azure wif
我正在使用WIF(.net 4.5)和Azure Active目录进行身份验证.该网站将位于Azure上.
一切都在本地工作,但是当我把它放到azure上时我得到错误:
数据保护操作失败.这可能是由于没有为当前线程的用户上下文加载用户配置文件引起的,这可能是线程模拟时的情况.
我理解这是因为应用程序无法使用DAPI,因此我需要切换到使用MAC保护我的应用程序.
在本地我把它添加到我的webconfig: -
<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" />
</securityTokenHandlers>
Run Code Online (Sandbox Code Playgroud)
正如文档中所建议的,我添加了一个静态机器密钥,但我找不到关于密钥长度的任何建议 - 所以我假设256.
但是,此配置只会出现此错误:
[CryptographicException:加密操作期间发生错误.] System.Web.Security.Cryptography.HomogenizingCryptoServiceWrapper.HomogenizeErrors(Func`2 func,Byte [] input)+115 System.Web.Security.Cryptography.HomogenizingCryptoServiceWrapper.Unprotect(Byte [] protectedData)+59 System.Web.Security.MachineKey.Unprotect(ICryptoServiceProvider cryptoServiceProvider,Byte [] protectedData,String []用途)+62 System.Web.Security.MachineKey.Unprotect(Byte [] protectedData,String []用途)+ 122 System.IdentityModel.Services.MachineKeyTransform.Decode(Byte [] encoded)+161 System.IdentityModel.Tokens.SessionSecurityTokenHandler.ApplyTransforms(Byte [] cookie,Boolean outbound)+123 System.IdentityModel.Tokens.SessionSecurityTokenHandler.ReadToken(XmlReader reader) ,SecurityTokenResolver tokenResolver)+575 System.IdentityModel.Tokens.SessionSecurityTokenHandler.ReadToken(Byte [] token,SecurityTokenResolver tokenResolver)+76 System.IdentityModel.Services.SessionAuthenticationMod ule.ReadSessionTokenFromCookie(Byte [] sessionCookie)+833 System.IdentityModel.Services.SessionAuthenticationModule.TryReadSessionTokenFromCookie(SessionSecurityToken&sessionToken)+186 System.IdentityModel.Services.SessionAuthenticationModule.OnAuthenticateRequest(Object sender,EventArgs eventArgs)+210 System.Web.SyncEventExecutionStep. System.Web.HttpApplication.IExecutionStep.Execute()+ 136 System.Web.HttpApplication.ExecuteStep(IExecutionStep step,Boolean&completedSynchronously)+69
我删除了machinekey部分,因为我没有指定格式正确的密钥,但错误不会消失.
什么是战斗WIF!
eXa*_*ier 48
如果未在配置中指定machineKey,则Azure会添加一个.但是,如果您使用VIP切换创建应用程序的新版本并将其部署到Azure,则Azure会为Staging中的部署生成新计算机密钥(假设您的第一次部署是生产).(VIP切换是部署新版本然后在生产和暂存之间切换虚拟IP地址的好机制).
因此,基本上一种解决方案是让Azure生成密钥,但在VIP切换后,您可以解决问题.为了避免它,您可以在Application_Error处理程序中捕获Global.asax中的CryptographicException,如下所示:
// Be sure to reference System.IdentityModel.Services
// and include using System.IdentityModel.Services;
// at the start of your class
protected void Application_Error(object sender, EventArgs e)
{
var error = Server.GetLastError();
var cryptoEx = error as CryptographicException;
if (cryptoEx != null)
{
FederatedAuthentication.WSFederationAuthenticationModule.SignOut();
Server.ClearError();
}
}
Run Code Online (Sandbox Code Playgroud)
SignOut()方法导致cookie被删除.
编辑:有关生成machineKey的更新信息,如@anjdreas所述.
另一个解决方案是生成machineKey,您可以使用IIS管理器来执行此操作,有关详细信息,请参阅生成MachineKey的最简单方法.如果将相同的密钥放入Azure Web角色中的所有Web应用程序中,Azure部署过程将不会替换它.
| 归档时间: |
|
| 查看次数: |
16974 次 |
| 最近记录: |