2013-10-14 12 views
12

में प्राधिकरण एट्रिब्यूट ओवरराइड करना मेरे आवेदन में, मैं अधिकृत उपयोगकर्ता को अपने प्रोफाइल पेज को अपडेट करने के लिए रीडायरेक्ट करना चाहता हूं जब तक कि उन्होंने आवश्यक जानकारी प्रदान नहीं की हो। यदि वे प्रोफ़ाइल अपडेट करते हैं, तो IsProfileCompleted डेटाबेस में 'सत्य' पर सेट है।एमवीसी 4

तो, मुझे पता है कि यह नियंत्रक की आवश्यक कार्रवाई में चेक स्थिति डालकर किया जा सकता है। लेकिन मैं AuthorizeAttribute को अनुकूलित करके ऐसा करना चाहता हूं।

मैं जानकारी के लिए Googled और 'StackOverflowed', लेकिन उलझन में मिला। कृपया मेरा मार्ग दर्शन कीजिए।

उत्तर

33
public class MyAuthorizeAttribute: AuthorizeAttribute 
{ 
    protected override bool AuthorizeCore(HttpContextBase httpContext) 
    { 
     var authorized = base.AuthorizeCore(httpContext); 
     if (!authorized) 
     { 
      // The user is not authorized => no need to go any further 
      return false; 
     } 

     // We have an authenticated user, let's get his username 
     string authenticatedUser = httpContext.User.Identity.Name; 

     // and check if he has completed his profile 
     if (!this.IsProfileCompleted(authenticatedUser)) 
     { 
      // we store some key into the current HttpContext so that 
      // the HandleUnauthorizedRequest method would know whether it 
      // should redirect to the Login or CompleteProfile page 
      httpContext.Items["redirectToCompleteProfile"] = true; 
      return false; 
     } 

     return true; 
    } 

    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) 
    { 
     if (filterContext.HttpContext.Items.Contains("redirectToCompleteProfile")) 
     { 
      var routeValues = new RouteValueDictionary(new 
      { 
       controller = "someController", 
       action = "someAction", 
      }); 
      filterContext.Result = new RedirectToRouteResult(routeValues); 
     } 
     else 
     { 
      base.HandleUnauthorizedRequest(filterContext); 
     } 
    } 

    private bool IsProfileCompleted(string user) 
    { 
     // You know what to do here => go hit your database to verify if the 
     // current user has already completed his profile by checking 
     // the corresponding field 
     throw new NotImplementedException(); 
    } 
} 

और उसके बाद आप इस कस्टम विशेषता वाले अपने नियंत्रक कार्यों को सजाने कर सकते हैं:

[MyAuthorize] 
public ActionResult FooBar() 
{ 
    ... 
} 
+0

मैं अपने एमवीसी ऐप में कस्टम प्राधिकरण के लिए इस उदाहरण का उपयोग कर रहा हूं। लेकिन, यह यूआरएल वापस करने के लिए इसे पुनर्निर्देशित नहीं करता है। क्या मुझे कुछ याद आया? – Ranger

+0

एक बार फिर मैं यहां हूं, समस्या से ऊपर हल हो गया है। मैं सत्र आधारित लॉगिन का उपयोग कर रहा हूं और कभी-कभी यह कोड निष्पादित करता है जिसे प्राधिकरण के बाद निष्पादित किया जाना चाहिए। मैं सत्र कुंजी के लिए स्थिर गुणों का उपयोग कर रहा हूँ। क्या आप इसमें मेरी सहायता कर सकते है? संरक्षित ओवरराइड bool AuthorizeCore (HttpContextBase HttpContext) { अगर (string.IsNullOrEmpty (CurrentUser.UserName) || CurrentUser.UserName == "") return false; सच वापसी; } – Ranger

+0

क्या यह उत्तर एमवीसी 5 के साथ संगत है या क्या हमें कोई बदलाव करना चाहिए? – radbyx

0

मैं इस कोड लिया और अपने परिवर्तनों में से कुछ जोड़ दिया है, अर्थात् जाँच करने के लिए करता है, तो वर्तमान में उपयोगकर्ता के प्रवेश सर्वर पर एक सत्र स्थिति है, वे उतना महंगा नहीं हैं जितना वे थे!

public class CustomAuthorizeAttribute : AuthorizeAttribute 
{ 
    protected override bool AuthorizeCore(HttpContextBase httpContext) 
    { 
     var authorized = base.AuthorizeCore(httpContext); 
     if (!authorized && !Membership.isAuthenticated()) 
     { 
      // The user is not authorized => no need to go any further 
      return false; 
     } 

     return true; 
    } 
} 
public class Membership 
{ 
    public static SystemUserDTO GetCurrentUser() 
    { 
     // create a system user instance 
     SystemUserDTO user = null; 

     try 
     { 
      user = (SystemUserDTO)HttpContext.Current.Session["CurrentUser"]; 
     } 
     catch (Exception ex) 
     { 
      // stores message into an event log 
      Utilities.Log(ex.Message, System.Diagnostics.EventLogEntryType.Warning); 

     } 
     return user; 
    } 

    public static bool isAuthenticated() 
    { 
     bool loggedIn = HttpContext.Current.User.Identity.IsAuthenticated; 
     bool hasSession = (GetCurrentUser() != null); 
     return (loggedIn && hasSession); 
    } 
}