2012-10-15 37 views
7

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?

Trả lời

5

Vâng, sau nhiều lần tìm kiếm, tôi đã tìm ra vấn đề của mình là gì. Trước khi tôi thiết lập ServiceConfigurationCreated, tôi đã thực hiện một số cấu hình gây ra quyền truy cập vào FederatedAuthentication.ServiceConfiguration. According to MSDN, "Sự kiện ServiceConfigurationCreated được nâng lên khi mô-đun HTTP đầu tiên trong ứng dụng web tham chiếu đến ServiceConfiguration". Tôi đã chuyển thiết lập trình xử lý sự kiện lên trên cùng của Application_Start và mọi thứ đã hoạt động như mong muốn, có nghĩa là sự kiện - chỉ xảy ra một lần - đã kích hoạt trước khi tôi có trình xử lý sự kiện được thiết lập.

Hy vọng điều này sẽ tiết kiệm cho người nào đó hơn 4 giờ để tôi chạy ứng dụng này.

+0

Wow! Nắm bắt tốt! bạn có của tôi lên trên đó! – astaykov

+0

Đẹ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

+0

@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ả). –

13

Lưu ý rằng bây giờ bạn có thể sử dụng MachineKeySessionSecurityTokenHandler để ký và mã hóa mã thông báo phiên trên các trang web.

Để sử dụng, bạn sẽ cần phải loại bỏ các mặc định SessionSecurityTokenHandler và thêm MachineKeySessionSecurityTokenHandler trong Web.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> 

Các MachineKeySessionSecurityTokenHandler tận dụng phím máy cấu hình trong Web.config vì vậy bạn sẽ cần phải thêm rằng quá:

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

Xem câu hỏi này trên BrainThud

+0

Mẹo hay. Tôi không chắc chắn điều này thực sự mua bất cứ điều gì trên phương pháp tiếp cận RSA, nhưng tôi vui mừng khi biết rằng nó có sẵn. –

+1

Đã cứu mạng sống của chúng tôi. Cảm ơn, người đàn ông. – SmartK8

+0

Khóa máy của tôi đã được định cấu hình nên đây là giải pháp vững chắc. –

Các vấn đề liên quan