2012-02-20 10 views
11

मैं कुछ कस्टम प्रमाणीकरण करने की कोशिश कर रहा हूं इसलिए मैंने OnAuthorization विधि को ओवरराइड करने वाला नियंत्रक बनाया। मैंने इस नियंत्रक को Authorize विशेषता भी लागू की। सवाल यह है कि OnAuthorization विधि मूल रूप प्रमाणीकरण प्रक्रिया से पहले क्यों कहा जाता है?प्रमाणीकरण से पहले प्राधिकरण निष्पादन क्यों कर रहा है?

मैं प्रमाणीकृत होने के बाद उपयोगकर्ता को अधिकृत करना चाहता हूं। क्या मुझे कुछ याद आ रही है?

यहाँ कोड है:

[Authorize] 
    public class AuthorizationController : Controller 
    { 
     protected override void OnAuthorization(AuthorizationContext filterContext) 
     { 
      base.OnAuthorization(filterContext); 

      if (filterContext == null) 
      { 
       throw new ArgumentNullException("filterContext"); 
      } 

      List<string> allowedControllers = new List<string>() { "SecurityController" }; 
      List<string> allowedActions = new List<string>() { "Index" }; 

      string controllerName = filterContext.Controller.GetType().Name; 
      string actionName = filterContext.ActionDescriptor.ActionName; 

      if (!allowedControllers.Contains(controllerName) 
      || !allowedActions.Contains(actionName)) 
      { 
       filterContext.Result = View("UnauthorizedAccess"); 
      } 
     } 
    } 

नियंत्रक है कि मैं के साथ परीक्षण किया है कुछ की तरह:

public class SecurityController : AuthorizationController 
{ 

    public ActionResult Index() 
    { 
     return View(); 
    } 

    public ActionResult AnotherIndex() 
    { 
     return View(); 
    } 
} 

उत्तर

15

पहले चीजों AuthorizeAttribute करता जाँच कर रहा है देखने के लिए में से एक है, तो उपयोगकर्ता को प्रमाणीकृत किया है । यदि वे तब नहीं हैं जब लॉगिन पृष्ठ पर रीडायरेक्ट जारी किया जाएगा।

protected virtual bool AuthorizeCore(HttpContextBase httpContext) { 
     if (httpContext == null) { 
      throw new ArgumentNullException("httpContext"); 
     } 

     IPrincipal user = httpContext.User; 
     if (!user.Identity.IsAuthenticated) { 
      return false; 
     } 

आप कोई भूमिकाओं के साथ AuthorizeAttribute का उपयोग करते हैं/उपयोगकर्ताओं के रूप में आप अपने उदाहरण ([अधिकृत]) में करते हैं, यह मूल रूप से बस है:

AuthorizeAttribute मूल रूप से प्रमाणीकरण प्राधिकरण टुकड़ा के साथ चेक इन लपेटता यह सुनिश्चित करने के लिए जांचें कि उपयोगकर्ता इस मामले में प्रमाणित है।

मैं शायद आपके कोडर को इस नियंत्रक में करने के बजाय AuthorizeAttribute को ओवरराइड करने के लिए बदल दूंगा। आप निम्न कार्य कर सकते हैं:

public class CustomAuthorizeAttribute : AuthorizeAttribute 
{ 
    public override void OnAuthorization(AuthorizationContext filterContext) 
    { 
     filterContext.Result = CreateResult(filterContext); 
    } 

    protected ActionResult CreateResult(AuthorizationContext filterContext) 
    { 
     var controllerContext = new ControllerContext(filterContext.RequestContext, filterContext.Controller); 
     var controller = (string)filterContext.RouteData.Values["controller"]; 
     var action = (string)filterContext.RouteData.Values["action"]; 
     // any custom model here 
     var model = new UnauthorizedModel(); 

     // custom logic to determine proper view here - i'm just hardcoding it 
     var viewName = "~/Views/Shared/Unauthorized.cshtml"; 

     return new ViewResult 
     { 
      ViewName = viewName, 
      ViewData = new ViewDataDictionary<UnauthorizedModel>(model) 
     }; 
    } 
} 
+0

ठीक है, ऐसा नहीं होता है क्योंकि मैं इसकी उम्मीद करता हूं। उपर्युक्त उदाहरण के लिए, जब मैं एक और इंडेक्स एक्शन तक पहुंचना चाहता हूं, तो मुझे लॉगिन पेज प्राप्त होने की उम्मीद है, लेकिन मुझे अनधिकृत एक्सेस प्राप्त हो रहा है। – misha

+0

आपके संपादन के बाद: मैं समझता हूं, लेकिन अगर मैं AuthorizeAttribute को ओवरराइड करता हूं, तो मुझे उपयोगकर्ता को किसी पृष्ठ पर रीडायरेक्ट करने जैसी अन्य कार्रवाइयां करने की पहुंच नहीं है जो उसे बताती है कि वह लॉग इन नहीं होने के बजाय अधिकृत नहीं है ... – misha

+0

@ मिशा ज़रूर तुम करना। आपको क्या लगता है कि आप नहीं कर सकते? – Dismissile

-2
public override void OnAuthorization(AuthorizationContext filterContext) 
{ 
    base.OnAuthorization(filterContext); 
    bool flag = false; 
    string UserId; 
    string[] AssignedRights = null; 

    //Check if Http Context Contains User Name 
    if (HttpContext.Current.User.Identity.Name != null && HttpContext.Current.User.Identity.Name != string.Empty) 
    { 
     //Get User Id from HttpContext 
     UserId = HttpContext.Current.User.Identity.Name; 
     RoleRepository roleRepository = new RoleRepository(); 
     AssignedRights = roleRepository.GetRolesByUser(Convert.ToInt32(UserId)); 
     flag = IsUserAuthorized(filterContext, flag, AssignedRights); 

     if (flag == false) 
     { 

      filterContext.Result = new HttpUnauthorizedResult(); 
     } 
    } 

} 
0

के बाद कस्टम प्राधिकरण गुण के लिए एक नमूना है।

public class AuthLogAttribute:AuthorizeAttribute 
    { 

     public string View { get; set; } 

     public AuthLogAttribute() 
     { 
      View = "AuthorizeFailed"; 
     } 
     public override void OnAuthorization(AuthorizationContext filterContext) 

     { 
      base.OnAuthorization(filterContext); 
      IsUserAuthorized(filterContext); 
     } 

     private void IsUserAuthorized(AuthorizationContext filterContext) 
     { 
      // If the Result returns null then the user is Authorized 
      if(filterContext.Result ==null) 
       return; 

      //If the user is Un-Authorized then Navigate to Auth Failed View 
      if(filterContext.HttpContext.User.Identity.IsAuthenticated) 

      { 
       var vr = new ViewResult(); 
       vr.ViewName = View; 

       ViewDataDictionary dict = new ViewDataDictionary(); 
       dict.Add("Message", "Sorry you are not Authorized to Perform this Action"); 
       vr.ViewData = dict; 

       var result = vr; 
       filterContext.Result = vr; 
      } 

     } 
    } 

आपका नियंत्रक निम्नलिखित तरह होगा,

[AuthLog(Roles ="Manager")]  
     public ActionResult Create() 
     { 
      var product = new Product(); 
      return View(product); 
     } 

अंत में नई साझा दृश्य कॉल "AuthorizeFailed" पैदा करते हैं।

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