2010-01-13 16 views
7

में 401.2 के लिए कस्टमरर्स मैंने सफलतापूर्वक एएसपी.NET में भूमिका आधारित प्राधिकरण को कार्यान्वित किया। जब किसी व्यक्ति की आवश्यक भूमिका नहीं होती है तो उसे 401.2 के लिए एक त्रुटि पृष्ठ दिखाई नहीं देता है।ASP.NET

अब मैं जो करना चाहता हूं वह है मेरे आवेदन में कस्टम 401 पेज होना और इसे web.config में सेटिंग्स के माध्यम से वहां रीडायरेक्ट करना है। मैंने यह कोशिश की:

<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm"> 
    <error statusCode="401" redirect="NoAccess.htm" /> 
</customErrors> 

लेकिन यह पकड़ा नहीं जाता है। क्या मुझे इसे आईआईएस में ओवरराइड करना है? मुझे आशा है कि इससे चीजों को कड़ी मेहनत नहीं मिल जाएगी।

उत्तर

7

मैं हाल ही में एक ही समस्या में भाग गया और यह पता चला कि विंडोज प्रमाणीकरण का उपयोग करते समय यह क्विर्क में से एक है।

जोशुआ फ्लानगन ने कुछ समय पहले nice HttpModule बनाया था जो आपके web.config में customErrors अनुभाग का सम्मान करेगा और 401 त्रुटि पृष्ठ पर रीडायरेक्ट करेगा।

कुंजी समाधान के लिए, पेज जीवनचक्र के EndRequest घटना रोकना एक 401 स्थिति कोड के लिए जांच करें और अपने कस्टम पेज पर अमल करने के लिए है।

HttpModule के पोर्टेबिलिटी अच्छा है क्योंकि यह समाधान पुन: प्रयोज्य बना देता है, और अपने Global.asax को साफ रखता है, लेकिन कुछ भी करता है, तो आप वास्तव में अपने कोड के साथ Global.asax में अपने EndRequest घटना तारों से क्या रोक रहा है चाहता था।

यदि आप एएसपी.नेट एमवीसी का उपयोग कर रहे हैं, तो समाधान काफी सुरुचिपूर्ण नहीं है।

3

आप एक HttpModule global.asax.cs में web.config

<system.web> 
    <customErrors mode="On" defaultRedirect="~/MyController/MyErrorAction/" redirectMode="ResponseRedirect"> 
     <error statusCode="401" redirect="~/MyController/MyErrorAction/" /> 
    </customErrors> 

में

जोड़ने के लिए नहीं करना चाहते हैं

protected void Application_EndRequest(object sender, EventArgs e) 
    { 
     HttpApplication application = (HttpApplication)sender; 

     if (application.Response.StatusCode != 401 || !application.Request.IsAuthenticated) return; 

     application.Response.ClearContent(); 

     //You can replace the piece below is to redirect using MVC, or your can replace all this with application.Server.Execute(yourPage); 
     IController errorController = new SharedController(); 
     var rd = new RouteData(); 
     rd.Values.Add("controller", "MyController"); 
     rd.Values.Add("action", "MyErrorAction"); 
     rd.Values.Add("value", "You or your user group do not have permissions to use the address: " + Request.Url.PathAndQuery); 

     errorController.Execute(new RequestContext(new HttpContextWrapper(Context), rd)); 
     HttpContext.Current.Server.ClearError(); 
    } 
+0

क्या IsAuthenticated की अस्वीकृति वास्तव में सही है? क्या यह दूसरी तरफ नहीं होना चाहिए, यानी अगर प्रमाणीकृत => विधि से वापस आना चाहिए? – aeliusd

3

यहाँ एक MVC नास्तिक संस्करण है:

वेब.कॉन्फिग

में 210
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm"> 
    <error statusCode="401" redirect="NoAccess.htm" /> 
</customErrors> 

Global.asax.cs

protected void Application_EndRequest(object sender, EventArgs e) 
{ 
    HttpApplication application = (HttpApplication)sender; 

    if (application.Response.StatusCode != 401 || !application.Request.IsAuthenticated) return; 

    var customErrors = (CustomErrorsSection)ConfigurationManager.GetSection("system.web/customErrors"); 

    var accessDeniedPath = customErrors.Errors["401"] != null ? customErrors.Errors["401"].Redirect : customErrors.DefaultRedirect; 
    if (string.IsNullOrEmpty(accessDeniedPath)) 
     return; // Let other code handle it (probably IIS). 

    application.Response.ClearContent(); 
    application.Server.Execute(accessDeniedPath); 
    HttpContext.Current.Server.ClearError(); 
} 
2

में यहां बताया गया है मेरे लिए अच्छी तरह से काम है।

Global.asax -

protected void Application_EndRequest(object sender, EventArgs e) 
    { 
     if (Response.StatusCode == 401 && Request.IsAuthenticated) 
     { 
      Response.StatusCode = 303; 
      Response.Clear(); 
      Response.Redirect("~/AccessDenied.html"); 
      Response.End(); 
     } 
    } 

Web.config -

<system.web> 
    <customErrors mode="On"> 
     <error statusCode="401" redirect="AccessDenied.html"/> 
    </customErrors> 
    <authentication mode="Windows"/> 
    </system.web> 
    <location path="AccessDenied.html"> 
    <system.web> 
     <authorization> 
     <allow roles="*"/> 
     </authorization> 
    </system.web> 
    </location> 
    <location path="."> 
    <system.web> 
     <authorization> 
     <allow roles="YourADGroup"/> 
     <deny users="*" /> 
     </authorization> 
    </system.web> 
    </location> 

यह रूप में अच्छी तरह से एक 200 समस्या से ठीक पहले डबल 401 का ख्याल रखता है। भी pesky फ़ायरफ़ॉक्स प्रमाणीकरण पॉपअप circumvents।