मैं कुछ कस्टम प्रमाणीकरण करने की कोशिश कर रहा हूं इसलिए मैंने 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();
}
}
ठीक है, ऐसा नहीं होता है क्योंकि मैं इसकी उम्मीद करता हूं। उपर्युक्त उदाहरण के लिए, जब मैं एक और इंडेक्स एक्शन तक पहुंचना चाहता हूं, तो मुझे लॉगिन पेज प्राप्त होने की उम्मीद है, लेकिन मुझे अनधिकृत एक्सेस प्राप्त हो रहा है। – misha
आपके संपादन के बाद: मैं समझता हूं, लेकिन अगर मैं AuthorizeAttribute को ओवरराइड करता हूं, तो मुझे उपयोगकर्ता को किसी पृष्ठ पर रीडायरेक्ट करने जैसी अन्य कार्रवाइयां करने की पहुंच नहीं है जो उसे बताती है कि वह लॉग इन नहीं होने के बजाय अधिकृत नहीं है ... – misha
@ मिशा ज़रूर तुम करना। आपको क्या लगता है कि आप नहीं कर सकते? – Dismissile