8

MSDN フォーラム、Dominic Baier のブログ、およびその他の情報源で、DPAPI は Azure でそのままでは機能しないこと、およびあらゆる種類の Web ファーム シナリオでフェデレーション認証を処理する 1 つのアプローチは、DPAPI 変換を置き換えることであると読みました。 X509 証明書を使用した RSA 暗号化など、ファーム全体で利用可能な秘密キーを使用するもの。Azure MVC アプリケーションでこのアプローチを採用し、次のSessionSecurityTokenHandlerように構成しました。

FederatedAuthentication.ServiceConfigurationCreated += (sender, args) =>
    {
        var sessionTransforms = new List<CookieTransform>(new CookieTransform[]
            {
                new DeflateCookieTransform(),
                new RsaEncryptionCookieTransform(args.ServiceConfiguration.ServiceCertificate),
                new RsaSignatureCookieTransform(args.ServiceConfiguration.ServiceCertificate)
            });
        var sessionHandler = new SessionSecurityTokenHandler(sessionTransforms.AsReadOnly());
        args.ServiceConfiguration.SecurityTokenHandlers.AddOrReplace(sessionHandler);                    
    };

この構成を使用すると、ID プロバイダーからトークンを受け取り、これらの変換を使用して暗号化された安全な Cookie を発行できます。Azure エミュレーターで実行すると、すべてが期待どおりに機能します。ただし、Azure 環境では、ブラウザーに次のエラーが断続的に表示されます。

Key not valid for use in specified state.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Security.Cryptography.CryptographicException: Key not valid for use in specified state.


Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[CryptographicException: Key not valid for use in specified state.
]
   System.Security.Cryptography.ProtectedData.Unprotect(Byte[] encryptedData, Byte[] optionalEntropy, DataProtectionScope scope) +577
   Microsoft.IdentityModel.Web.ProtectedDataCookieTransform.Decode(Byte[] encoded) +80

[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. ]
   Microsoft.IdentityModel.Web.ProtectedDataCookieTransform.Decode(Byte[] encoded) +433
   Microsoft.IdentityModel.Tokens.SessionSecurityTokenHandler.ApplyTransforms(Byte[] cookie, Boolean outbound) +189
   Microsoft.IdentityModel.Tokens.SessionSecurityTokenHandler.ReadToken(XmlReader reader, SecurityTokenResolver tokenResolver) +862
   Microsoft.IdentityModel.Tokens.SessionSecurityTokenHandler.ReadToken(Byte[] token, SecurityTokenResolver tokenResolver) +109
   Microsoft.IdentityModel.Web.SessionAuthenticationModule.ReadSessionTokenFromCookie(Byte[] sessionCookie) +356
   Microsoft.IdentityModel.Web.SessionAuthenticationModule.TryReadSessionTokenFromCookie(SessionSecurityToken& sessionToken) +123
   Microsoft.IdentityModel.Web.SessionAuthenticationModule.OnAuthenticateRequest(Object sender, EventArgs eventArgs) +61
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +80
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +270

これは、 が DPAPI を使用して Cookie を復号化しようとしていることを示唆しているようSessionSecurityTokenHandlerですが、なぜですか? 上記のRSAを使用するように構成しませんでしたか?

4

2 に答える 2

15

MachineKeySessionSecurityTokenHandlerを使用して、 Web ファーム間でセッション トークンに署名および暗号化できるようになったことに注意してください。

これを使用するには、デフォルトを削除してinSessionSecurityTokenHandlerを追加する必要があります。MachineKeySessionSecurityTokenHandlerWeb.config

<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" />
    </securityTokenHandlers>
  </identityConfiguration>
</system.identityModel>

MachineKeySessionSecurityTokenHandlerで構成されたマシン キーを使用するWeb.configため、それも追加する必要があります。

<system.web>
  <machineKey validationKey="..." decryptionKey="..." validation="SHA1" decryption="AES" />
</system.web>

BrainThudでこの質問を参照してください

于 2013-05-20T15:52:32.683 に答える
5

さて、多くの検索の後、私は自分の問題が何であるかを理解しました。を設定する前にServiceConfigurationCreated、へのアクセスを引き起こすいくつかの構成を行っていましたFederatedAuthentication.ServiceConfigurationMSDNによると、「Webアプリケーションの最初のHTTPモジュールがServiceConfigurationを参照すると、ServiceConfigurationCreatedイベントが発生します」。イベントハンドラーの設定を一番上に移動するApplication_Startと、すべてが正常に機能しました。つまり、イベントハンドラーを設定する前に、イベント(1回だけ発生する)が発生していました。

うまくいけば、これは私がこれを地面に実行するのにかかった4時間以上誰かを節約するでしょう。

于 2012-10-16T00:21:21.003 に答える