कहा जाता है, मैं डब्ल्यूसीएफ में लागू एक आरईएसटी सेवा के लिए प्रमाणीकरण को लागू करने और Azure पर होस्ट करने का प्रयास कर रहा हूं। मैं प्रमाणीकरण Request, PostAuthenticationRequest और EndRequest घटनाओं को संभालने के लिए HttpModule का उपयोग कर रहा हूं। अगर प्राधिकरण शीर्षलेख गुम है या यदि उसमें निहित टोकन अमान्य है, तो EndRequest के दौरान मैं 401 पर प्रतिक्रिया पर स्टेटस कोड स्थापित कर रहा हूं। हालांकि, मैंने यह निर्धारित किया है कि एंड्रॉक्वेट को दो बार बुलाया जाता है, और दूसरी कॉल पर प्रतिक्रिया में पहले से ही हेडर होते हैं सेट करें, जो कोड को स्टेटसोड को अपवाद फेंकने के लिए सेट करता है।HttpModule EndRequest हैंडलर को दो बार
मैंने यह सुनिश्चित करने के लिए इनक() में ताले जोड़े हैं कि हैंडलर को दो बार पंजीकृत नहीं किया जा रहा था; अभी भी दो बार भाग गया। Init() भी दो बार भाग गया, यह दर्शाता है कि HttpModule के दो उदाहरण बनाए जा रहे थे। हालांकि, वीएस डीबगर में सेट ऑब्जेक्ट आईडी का उपयोग करना इंगित करता है कि अनुरोध वास्तव में अलग-अलग अनुरोध हैं। मैंने फिडलर में सत्यापित किया है कि ब्राउज़र से मेरी सेवा के लिए केवल एक ही अनुरोध जारी किया जा रहा है।
यदि मैं डब्ल्यूसीएफ सेवा होस्ट कॉन्फ़िगरेशन के आधार पर ग्लोबल.एक्सएक्स रूटिंग का उपयोग करने के लिए स्विच करता हूं, तो हैंडलर को केवल एक बार बुलाया जाता है और सब कुछ ठीक काम करता है।
यदि मैं system.web कॉन्फ़िगरेशन अनुभाग के साथ-साथ system.web सर्वर कॉन्फ़िगरेशन अनुभाग में कॉन्फ़िगरेशन को जोड़ता हूं, तो हैंडलर को केवल एक बार बुलाया जाता है और सब कुछ ठीक काम करता है।
तो मेरे पास कमजोरियां हैं, लेकिन मैं वास्तव में व्यवहार से नापसंद हूं जिसे मैं समझ नहीं पा रहा हूं। हैंडलर को दो बार क्यों बुलाया जाता है?
Web.config:
यहाँ समस्या का एक न्यूनतम रेप्रो है
<system.web>
<compilation debug="true" targetFramework="4.0" />
<!--<httpModules>
<add name="AuthModule" type="TestWCFRole.AuthModule, TestWCFRole"/>
</httpModules>-->
</system.web>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="WebBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true" />
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
<services>
<service name="TestWCFRole.Service1">
<endpoint binding="webHttpBinding" name="RestEndpoint" contract="TestWCFRole.IService1" bindingConfiguration="HttpSecurityBinding" behaviorConfiguration="WebBehavior"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost/" />
</baseAddresses>
</host>
</service>
</services>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
</webHttpEndpoint>
</standardEndpoints>
<bindings>
<webHttpBinding>
<binding name="HttpSecurityBinding" >
<security mode="None" />
</binding>
</webHttpBinding>
</bindings>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="AuthModule" type="TestWCFRole.AuthModule, TestWCFRole"/>
</modules>
<directoryBrowse enabled="true"/>
</system.webServer>
Http मॉड्यूल:
using System;
using System.Web;
namespace TestWCFRole
{
public class AuthModule : IHttpModule
{
/// <summary>
/// You will need to configure this module in the web.config file of your
/// web and register it with IIS before being able to use it. For more information
/// see the following link: http://go.microsoft.com/?linkid=8101007
/// </summary>
#region IHttpModule Members
public void Dispose()
{
//clean-up code here.
}
public void Init(HttpApplication context)
{
// Below is an example of how you can handle LogRequest event and provide
// custom logging implementation for it
context.EndRequest += new EventHandler(OnEndRequest);
}
#endregion
public void OnEndRequest(Object source, EventArgs e)
{
HttpContext.Current.Response.StatusCode = 401;
}
}
}
आप अपने आवेदन में UrlRewrite उपयोग करते हैं? ऐसा लगता है कि यह एंड्रॉवेस्ट को दो बार आग लगने का कारण बनता है। –
क्या यह पृष्ठभूमि में सक्षम है? मेरे पास urlRrriteModule मेरे web.config में नहीं है। –
मुझे ऐसा नहीं लगता है। यह वास्तव में अजीब है, क्योंकि मैं यह नहीं कह सकता कि मेरी सभी समस्या UrlRewiteModule के कारण होती है। लेकिन उनमें से कुछ हैं। –