【发布时间】:2012-12-16 16:43:49
【问题描述】:
我正在使用 WIF (.net 4.5) 和 Azure Active Directory 进行身份验证。该网站将位于 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>
按照documentation 中的建议,我添加了一个静态机器密钥,但我找不到任何关于密钥长度的建议 - 所以我假设为 256。
然而,这个配置只是给出了这个错误:
[CryptographicException:加密操作期间发生错误。] System.Web.Security.Cryptography.HomogenizingCryptoServiceWrapper.HomogenizeErrors(Func`2 func, Byte[] 输入) +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(字节 [] 编码)+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.SessionAuthenticationModule.ReadSessionTokenFromCookie(Byte[] sessionCookie) +833 System.IdentityModel.Services.SessionAuthenticationModule.TryReadSessionTokenFromCookie(SessionSecurityToken& sessionToken) +186 System.IdentityModel.Services.SessionAuthenticationModule.OnAuthenticateRequest(对象发送者,EventArgs eventArgs)+210 System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +136 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +69
如果我没有指定格式正确的密钥,我删除了 machinekey 部分,但错误并没有消失。
WIF 是一场多么激烈的战斗!
【问题讨论】:
-
为了解决我的问题,我将其添加到 web.config 并使用此站点生成了一个机器密钥aspnetresources.com/tools/machineKey