2009-02-16 12 views
12

एमवीसी में मौजूदा प्राधिकृत विशेषता को बढ़ाने के लिए मैं एक कस्टम विशेषता कैसे बना सकता हूं?asp.net mvc AUTHORIZE विशेषता में जोड़ना

+0

कृपया अधिक जानकारी जोड़ें, आप वास्तव में क्या विस्तार करना चाहते हैं? –

+0

अब के लिए मैं सिर्फ डिफ़ॉल्ट होम पेज की बजाय सही पृष्ठ पर रीडायरेक्ट करने में सक्षम होना चाहता हूं। – zsharp

+5

आप अपना प्रश्न अपडेट कर सकते हैं, इसलिए सभी को यह जानने में सक्षम होगा कि आपको क्या चाहिए। –

उत्तर

17

AuthorizeAttribute से अपनी कक्षा को प्राप्त करें। ऑन-प्राधिकरण विधि को ओवरराइड करें। एक कैश वैलिडेशन हैंडलर जोड़ें और सेट करें।

public void CacheValidationHandler(HttpContext context, 
            object data, 
            ref HttpValidationStatus validationStatus) 
{ 
    validationStatus = OnCacheAuthorization(new HttpContextWrapper(context)); 
} 


public override void OnAuthorization(AuthorizationContext filterContext) 
{ 
    if (filterContext == null) 
    { 
     throw new ArgumentNullException("filterContext"); 
    } 

    if (AuthorizeCore(filterContext.HttpContext)) 
    { 
     ... your custom code ... 
     SetCachePolicy(filterContext); 
    } 
    else if (!filterContext.HttpContext.User.Identity.IsAuthenticated) 
    { 
     // auth failed, redirect to login page 
     filterContext.Result = new HttpUnauthorizedResult(); 
    } 
    else 
    { 
     ... handle a different case than not authenticated 
    } 
} 


protected void SetCachePolicy(AuthorizationContext filterContext) 
{ 
    // ** IMPORTANT ** 
    // Since we're performing authorization at the action level, the authorization code runs 
    // after the output caching module. In the worst case this could allow an authorized user 
    // to cause the page to be cached, then an unauthorized user would later be served the 
    // cached page. We work around this by telling proxies not to cache the sensitive page, 
    // then we hook our custom authorization code into the caching mechanism so that we have 
    // the final say on whether a page should be served from the cache. 
    HttpCachePolicyBase cachePolicy = filterContext.HttpContext.Response.Cache; 
    cachePolicy.SetProxyMaxAge(new TimeSpan(0)); 
    cachePolicy.AddValidationCallback(CacheValidationHandler, null /* data */); 
} 
+0

ठीक है, लेकिन मैं वास्तव में अंतिम पृष्ठ पर सही तरीके से रीडायरेक्ट कैसे कर सकता हूं? – zsharp

+0

मैं इस काम को भूमिकाओं के साथ कैसे बना सकता हूं? यह अभी ठीक काम करता है लेकिन लगता है कि भूमिकाएं काम नहीं कर रही हैं। इसके अलावा AuthorizeCore उपयोगकर्ता को प्रमाणीकृत होने पर भी झूठी वापसी करता रहता है जिसका अर्थ है SetCachePolicy() कभी निष्पादित नहीं होता है। –

+0

@ निक - मैंने तब से कैश हैंडलिंग पहलुओं में सुधार के बारे में ब्लॉग किया है: http://farm-fresh-code.blogspot.com/2011/03/revisiting-custom-authorization-in.html – tvanfosson

3
public class CoolAuthorizeAttribute : AuthorizeAttribute 
{ 
} 
10

आपको इस विशेषता को बढ़ाने की आवश्यकता नहीं है, web.config पर्याप्त है। कृपया forms Element for authentication के बारे में पढ़ें। डिफ़ॉल्ट यूआरएल पर अपना ध्यान दें। यह कुछ है जो आपको चाहिए।

<system.web> 
    <authentication mode="Forms"> 
    <forms defaultUrl="YourUrlGoesHere"/> 
    </authentication> 
</system.web> 
+0

लेकिन यह गतिशील नहीं है। यूआरएल परिवर्तन। – zsharp

+7

एचएम, समाधान देने से पहले सभी आवश्यकताएं क्यों निर्दिष्ट नहीं करें? –

+1

नोओ! यह पूरी तरह गलत है: http://blogs.msdn.com/b/rickandy/archive/2010/08/24/securing-your-mvc-application.aspx – bplus

0

मेरा सुझाव है अगर आप सिर्फ वर्तमान AuthorizeAttribute का विस्तार करने और बजाय अधिभावी OnAuthorization सिर्फ AuthorizeCore ओवरराइड और यह करने के लिए अपने MyCustomAuthorizationHolds शर्त जोड़ के उस के शीर्ष पर अपने खुद के प्राधिकरण जोड़ने के लिए, चाहते हैं।

public class CustomAuthorizeAttribute : AuthorizeAttribute 
{ 
    // This method must be thread-safe since it is called by the thread-safe OnCacheAuthorization() method. 
    protected override bool AuthorizeCore(HttpContextBase httpContext) 
    { 
     if (base.AuthorizeCore(httpContext) && MyCustomAuthorizationHolds) 
      return true; 

     return false; 
    } 
}