2012-10-15 11 views
7

मैंने एमएसडीएन मंचों, डोमिनिक बायर के ब्लॉग में पढ़ा है, और अन्य स्रोतों में कि डीपीएपीआई एज़ूर में बॉक्स के बाहर काम नहीं करेगा, और किसी भी प्रकार के वेब फार्म परिदृश्य में संघीय प्रमाणीकरण को संभालने का एक दृष्टिकोण है डीपीएपीआई एक ऐसे व्यक्ति के साथ बदलता है जो खेत में उपलब्ध एक निजी कुंजी का उपयोग करता है, जैसे X509 प्रमाणपत्र का उपयोग कर आरएसए एन्क्रिप्शन। मैं अपने 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);      
    }; 

इस विन्यास का उपयोग करना, हम कोई पहचान प्रदाता से टोकन प्राप्त करते हैं और सुरक्षित कुकीज़ इन रूपांतरण का उपयोग करके एन्क्रिप्ट जारी करने के लिए सक्षम हैं। 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 

यह पता चलता है कि SessionSecurityTokenHandler कुकी DPAPI का उपयोग कर डिक्रिप्ट करने के लिए कोशिश कर रहा है, लेकिन क्यों लगता है? क्या मैंने ऊपर आरएसए का उपयोग करने के लिए इसे कॉन्फ़िगर नहीं किया?

उत्तर

5

ठीक है, बहुत खोज के बाद, मुझे पता चला कि मेरी समस्या क्या थी। ServiceConfigurationCreated सेट करने से पहले, मैं कुछ कॉन्फ़िगरेशन कर रहा था जिसके कारण FederatedAuthentication.ServiceConfiguration तक पहुंच हुई। According to MSDN, "सेवा कॉन्फ़िगरेशनक्रेटेड ईवेंट तब उठाया जाता है जब वेब अनुप्रयोग में पहला HTTP मॉड्यूल सेवा कॉन्फ़िगरेशन संदर्भित करता है"। मैंने इवेंट हैंडलर सेटअप को Application_Start के शीर्ष पर ले जाया और सबकुछ इसके रूप में काम किया जाना चाहिए, जिसका अर्थ यह है कि घटना - जो केवल एक बार आग लगती है - ईवेंट हैंडलर सेट होने से पहले फायरिंग कर रहा था।

उम्मीद है कि यह किसी को जमीन पर चलाने के लिए मुझे 4+ घंटे बचाएगा।

+0

वाह पर इस सवाल मिलते हैं! अच्छी पकड़! तुम उस पर मेरा हो! – astaykov

+0

अच्छा !!! लेकिन आपने कॉन्फ़िगरेशन फ़ाइल के माध्यम से क्यों नहीं किया? 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-protecteddedd-api.aspx – antwoord

+0

@antwoord जो इसे स्थापित करने के लिए अधिक तार्किक तरीका होगा। मुझे लगता है कि यह 'Application_Start' हैंडलर में किया गया कारण था क्योंकि जिस व्यक्ति ने इसे नहीं किया था, उसे यह नहीं पता था कि यह' web.config' में किया जा सकता है, और यह कोई मुद्दा नहीं था जिसे मैं बदलना चाहता था (हालांकि यह सब के बाद कम समय ले सकता है)। –

13

ध्यान दें कि अब आप वेब खेतों में सत्र टोकन को साइन और एन्क्रिप्ट करने के लिए MachineKeySessionSecurityTokenHandler का उपयोग कर सकते हैं।

इस का उपयोग करने के लिए, यदि आप डिफ़ॉल्ट SessionSecurityTokenHandler को हटाने और जोड़ने की आवश्यकता होगी Web.config में MachineKeySessionSecurityTokenHandler:

<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

+0

अच्छी टिप। मुझे यकीन नहीं है कि यह वास्तव में आरएसए दृष्टिकोण पर कुछ भी खरीदता है, लेकिन मुझे यह जानकर खुशी हो रही है कि यह उपलब्ध है। –

+1

हमारे जीवन को बचाया। धन्यवाद दोस्त। – SmartK8

+0

मेरी मशीन कुंजी पहले ही कॉन्फ़िगर की गई थी इसलिए यह एक ठोस समाधान है। –

संबंधित मुद्दे