SessionSecurityTokenHandler próbuje odszyfrować SessionSecurityToken w pliku cookie zaszyfrowanym RSA przy użyciu DPAPI; czemu?
Przeczytałem na forach MSDN, blogu Dominica Baiera, aw innych źródłach, że DPAPI nie będzie działać od razu w Azure, i że jednym ze sposobów obsługi uwierzytelniania federacyjnego w dowolnym scenariuszu farmy internetowej jest zastąpienie transformacji DPAPI przy użyciu klucza prywatnego dostępnego w całej farmie, takiego jak szyfrowanie RSA przy użyciu certyfikatu X509. Podjąłem to podejście w mojej aplikacji Azure MVC i skonfigurowałemSessionSecurityTokenHandler
lubię to:
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);
};
Korzystając z tej konfiguracji, jesteśmy w stanie odbierać tokeny od dostawcy tożsamości i wydawać bezpieczne pliki cookie zaszyfrowane przy użyciu tych transformacji. Działając w emulatorze Azure, wszystko działa zgodnie z oczekiwaniami. Jednak w środowisku Azure sporadycznie wyświetlany jest następujący błąd w przeglądarce:
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
To wydaje się sugerować, żeSessionSecurityTokenHandler
próbuje odszyfrować plik cookie za pomocą DPAPI, ale dlaczego? Czy nie skonfigurowałem go do używania RSA powyżej?