Tôi đã đọc trong diễn đàn MSDN, blog của Dominic Baier và trong các nguồn khác mà DPAPI sẽ không hoạt động ngoài Azure, và cách tiếp cận để xử lý xác thực liên kết trong bất kỳ loại kịch bản trang trại nào là thay thế DPAPI chuyển đổi với một trong đó sử dụng một khóa riêng có sẵn trên trang trại, chẳng hạn như mã hóa RSA bằng cách sử dụng một chứng chỉ X509. Tôi đã thực hiện phương pháp này trong ứng dụng Azure MVC của tôi và cấu hình SessionSecurityTokenHandler
như thế này:SessionSecurityTokenHandler cố gắng giải mã SessionSecurityToken trong cookie được mã hóa RSA bằng DPAPI; tại sao?
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);
};
Sử dụng cấu hình này, chúng tôi có thể nhận được thẻ từ nhà cung cấp nhận dạng và phát hành các tập tin cookie an toàn mã hóa bằng những biến đổi. Chạy trong trình giả lập Azure, mọi thứ hoạt động như mong đợi. Tuy nhiên, trong môi trường Azure, chúng tôi liên tục nhìn thấy các lỗi sau đây trong trình duyệt:
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
Điều này dường như cho thấy SessionSecurityTokenHandler
đang cố gắng để giải mã các tập tin cookie sử dụng DPAPI, nhưng tại sao? Tôi đã không cấu hình nó để sử dụng RSA ở trên?
Wow! Nắm bắt tốt! bạn có của tôi lên trên đó! – astaykov
Đẹp !!! Nhưng tại sao bạn không làm điều đó thông qua tập tin cấu hình? http://blogs.msdn.com/b/distributedservices/archive/2012/10/29/wif-1-0-id1073-a-cryptographicexception-occurred-when-attempting-to-decrypt-the-cookie-using- the-protecteddata-api.aspx – antwoord
@antwoord đó sẽ là cách hợp lý hơn để thiết lập nó. Tôi nghĩ lý do nó được thực hiện trong trình xử lý 'Application_Start' là bởi vì người đã làm nó không nhận ra nó có thể được thực hiện trong' web.config', và nó không phải là vấn đề tôi muốn thay đổi (mặc dù nó có thể mất ít thời gian hơn sau khi tất cả). –