簡體   English   中英

使用ACS擴展Azure Web角色時的保護API異常

[英]Protection API Exceptions when scaling Azure Web Roles using ACS

我一直在使用針對Windows Live和Google的Azure ACS,它一直運行沒有任何問題。 昨晚我們將實例從1個運行實例擴展到3個,從那以后人們在訪問我們的網站時報告了問題。 我們已將此跟蹤到以下異常情況,該異常情況經常發生。

我們假設我們的配置中存在問題,但不確定我們缺少什么。 我們設置機器密鑰......

<machineKey decryption="AES" decryptionKey="F7_SOMETHING_SOMETHING_FA" validation="SHA1" validationKey="63_SOMETHING_SOMETHING_BF" />

任何人都可以解釋這個問題嗎?

System.InvalidOperationException: ID1073: A CryptographicException occurred when attempting to decrypt the cookie using the ProtectedData API (see inner exception for details). If you are using IIS 7.5, this could be due to the loadUserProfile setting on the Application Pool being set to false.  ---> System.Security.Cryptography.CryptographicException: Key not valid for use in specified state.

   at System.Security.Cryptography.ProtectedData.Unprotect(Byte[] encryptedData, Byte[] optionalEntropy, DataProtectionScope scope)
   at Microsoft.IdentityModel.Web.ProtectedDataCookieTransform.Decode(Byte[] encoded)
   --- End of inner exception stack trace ---
   at Microsoft.IdentityModel.Web.ProtectedDataCookieTransform.Decode(Byte[] encoded)
   at Microsoft.IdentityModel.Tokens.SessionSecurityTokenHandler.ApplyTransforms(Byte[] cookie, Boolean outbound)
   at Microsoft.IdentityModel.Tokens.SessionSecurityTokenHandler.ReadToken(XmlReader reader, SecurityTokenResolver tokenResolver)
   at Microsoft.IdentityModel.Tokens.SessionSecurityTokenHandler.ReadToken(Byte[] token, SecurityTokenResolver tokenResolver)
   at Microsoft.IdentityModel.Web.SessionAuthenticationModule.ReadSessionTokenFromCookie(Byte[] sessionCookie)
   at Microsoft.IdentityModel.Web.SessionAuthenticationModule.TryReadSessionTokenFromCookie(SessionSecurityToken& sessionToken)
   at Microsoft.IdentityModel.Web.SessionAuthenticationModule.OnAuthenticateRequest(Object sender, EventArgs eventArgs)
   at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

注意:對於上下文。 這是在Windows Azure Web角色中運行的,是MVC 4。

現在您已經擴展,並且由於您的應用程序托管在負載均衡器后面,因此您的用戶可能導航到服務器A,在服務器A上獲得受DPAPI保護的會話cookie,但是當他們繼續瀏覽站點時負載均衡器重定向請求以在服務器B上執行。發生這種情況時,服務器B沒有匹配的機器密鑰,因此它無法解密會話cookie並拋出上述錯誤。 您可以通過以下三種方式解決此問題。

Windows Identity Foundation(WIF)是一個必須安裝在計算機上的帶外運行時,以便您的聲明感知應用程序可以使用它。 默認情況下,Windows Azure實例上未安裝WIF。 要運行雲聲明感知應用程序,必須在Windows Azure實例上提供WIF運行時。 最簡單的方法是將WIF程序集包含在部署包中。

將WIF程序集包含在Windows Azure部署程序包中

  1. 在解決方案資源管理器中,找到聲明感知應用程序。
  2. 展開References文件夾。
  3. 在References文件夾下找到Microsoft.IdentityModel程序集。
  4. 右鍵單擊該程序集,然后單擊“屬性”。
  5. 在屬性窗口中,將Copy Local as True和Specific Version指定為False。

默認情況下,WIF使用數據保護應用程序編程接口(DPAPI)以加密方式保護cookie。 Windows Azure上不提供DPAPI。 要確保您的雲聲明感知Web應用程序在部署到Windows Azure時能夠正常運行,您必須使用RSA添加Cookie加密功能。

使用RSA加密cookie

  1. 在解決方案資源管理器中,找到您的雲聲明感知Web應用程序。
  2. 在Visual Studio編輯器中打開global.asax.cs文件,該文件是global.asax文件背后的代碼。

添加以下聲明:

using Microsoft.IdentityModel.Tokens;
using Microsoft.IdentityModel.Web;
using Microsoft.IdentityModel.Web.Configuration;

添加以下代碼:

void OnServiceConfigurationCreated(object sender, ServiceConfigurationCreatedEventArgs e)
{
    //
    // Use the <serviceCertificate> to protect the cookies that are
    // sent to the client.
    //
    List<CookieTransform> sessionTransforms =
        new List<CookieTransform>(new CookieTransform[] {
        new DeflateCookieTransform(), 
        new RsaEncryptionCookieTransform(e.ServiceConfiguration.ServiceCertificate),
        new RsaSignatureCookieTransform(e.ServiceConfiguration.ServiceCertificate)  });
    SessionSecurityTokenHandler sessionHandler = new SessionSecurityTokenHandler(sessionTransforms.AsReadOnly());
    e.ServiceConfiguration.SecurityTokenHandlers.AddOrReplace(sessionHandler);
}

void Application_Start(object sender, EventArgs e)
{
    FederatedAuthentication.ServiceConfigurationCreated += OnServiceConfigurationCreated;

更多信息可以在這里找到: http//msdn.microsoft.com/en-us/library/hh289318.aspx

farm環境中使用WIF時,這是常見的例外。 關鍵是默認行為是使用DPAPI加密cookie。 但是DPAPI是MachineKey綁定的。

您必須在Global.Asax中進行一些小改動,並使用RSA Crypto服務提供程序來加密/解密FedAuth Cookie。 看看這篇關於如何實現這一目標的文章

在嘗試使用部署為Windows Azure網站實例的MVC 4應用程序與雲服務時,我遇到了類似的問題。 以下幫助我解決了這個問題。 http://msdn.microsoft.com/en-us/library/hh568644.aspx

滾動到最底部,找到顯示刪除SessionSecurityTokenHandler並將其替換為MachineKeySessionSecurityTokenHandler的示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM