2010-12-16 12 views
31

एएसपीनेट एमवीसी 2 response 302 के साथ लॉगिन पृष्ठ पर रीडायरेक्ट करता है जब प्रमाणित उपयोगकर्ता के पास कोई अधिकार नहीं है।एएसपीनेट एमवीसी प्राधिकृत विशेषता, कस्टम "कोई अधिकार नहीं" पृष्ठ पर रीडायरेक्ट करें

मैं दो कार्यों

  1. उपयोगकर्ता प्रमाणीकृत नहीं है, तो क्या यह करता है, प्रवेश पृष्ठ पर पुनर्निर्देशित कर में विभाजित करना चाहते हैं।
  2. यदि उपयोगकर्ता प्रमाणित है लेकिन उसके पास कोई आवश्यक अधिकार नहीं है तो उचित http स्थिति कोड वापस करें और कोई अधिकार दोस्त पृष्ठ दिखाएं।

क्या ऐसा करने का कोई तरीका है? या मैं प्रमाणीकरण और प्रमाणीकरण के साथ कुछ गलत कर रहा हूँ? एकमात्र तरीका जिसे मैं सोच सकता हूं वह कस्टम प्राधिकरण विशेषता लिखकर है, जिसे मैं टालना चाहता हूं।

+2

हाहा "कोई अधिकार दोस्त" पृष्ठ ... lol –

उत्तर

9

आप एक कस्टम प्राधिकृत विशेषता लिख ​​सकते हैं और AuthorizeCore विधि में यदि उपयोगकर्ता प्रमाणीकृत नहीं है तो HttpUnauthorizedResult लौटाएं और यदि वह प्रमाणीकृत है लेकिन भूमिकाओं में नहीं है तो आप कुछ अन्य कार्यवाही करेंगे। ध्यान दें कि यदि आप 401 स्टेटस कोड लौटाते हैं तो फॉर्म प्रमाणीकरण फ्रेमवर्क अंततः लॉगिन पेज पर 302 के साथ रीडायरेक्ट करेगा।

+4

लेकिन ... आप केवल AuthorizeCore से एक बूल वापस कर सकते हैं। आपको ActionResult –

+1

@EduardoMolteni को वापस करने के लिए ऑन-प्राधिकरण को ओवरराइड करना होगा, आप जो परिणाम चाहते हैं उसे वापस करने के लिए आप 'हैंडलयूनार्डिज्ड रिक्वेस्ट' विधि को ओवरराइड कर सकते हैं। –

18

आप इस तरह कस्टम फ़िल्टर विशेषता लिख ​​सकते हैं:

public class CustomAuthorizeAttribute : ActionFilterAttribute 
    { 
     public override void OnActionExecuting(ActionExecutingContext filterContext) 
     { 
      if (filterContext.HttpContext.User.Identity == null || !filterContext.HttpContext.User.Identity.IsAuthenticated) 
      { 
       filterContext.Result = new RedirectResult(System.Web.Security.FormsAuthentication.LoginUrl + "?returnUrl=" + 
       filterContext.HttpContext.Server.UrlEncode(filterContext.HttpContext.Request.RawUrl)); 
      } 

      //Check user right here 
      if (userNotRight) 
      { 
       filterContext.HttpContext.Response.StatusCode = 302; 
       filterContext.Result = new HttpUnauthorizedResult(); 
      } 
     } 
    } 

और नियंत्रक में उपयोग:

[CustomAuthorize] 
public class HomeController : Controller 
{ 

} 
+18

प्राधिकरण के उद्देश्य के लिए आपको ActionFilterAttribute की बजाय AuthorizeAttribute का उपयोग करना चाहिए। – ReinierDG

+1

हाँ, आप सही हैं। – hellangle

+0

@ReinierDG तो, AuthorizeAttribute के साथ इसे कैसे कार्यान्वित करें? – Bellash

7

Customizing authorization in ASP.NET MVC में सुझाव के रूप में, आप AuthorizeAttribute उपवर्ग सकता प्रमाणीकृत-लेकिन- रोकना अनधिकृत परिदृश्य और परिणाम को एक रीडायरेक्ट के साथ प्रतिस्थापित करें।

+0

यह विशेष रूप से कैश किए गए परिणामों को अधिकृत करने के लिए बेहतर है। –

6

एक कस्टम AuthorizeAttribute लागू करें और निम्न ओवरराइड जोड़ें। मूल बातें यह जांचना है कि क्या उपयोगकर्ता प्रमाणीकृत है लेकिन अधिकृत नहीं है और फिर अपने आप "एक्सेस अस्वीकृत" पृष्ठ पर रीडायरेक्ट करें। उम्मीद है की यह मदद करेगा!

public override void OnAuthorization(AuthorizationContext filterContext) 
{ 
    base.OnAuthorization(filterContext); 

    // Check if user is authenticated and if this action requires authorization 
    if (filterContext.HttpContext.User.Identity.IsAuthenticated 
     && filterContext.ActionDescriptor.IsDefined(typeof(AuthorizeAttribute), true) 
     || filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AuthorizeAttribute), true)) 
    { 
     List<object> attributes = new List<object>(filterContext.ActionDescriptor.GetCustomAttributes(typeof(AuthorizeAttribute), true)); 
     attributes.AddRange(filterContext.ActionDescriptor.ControllerDescriptor.GetCustomAttributes(typeof(AuthorizeAttribute), true)); 

     // Check all authorzation attributes 
     foreach (var attribute in attributes) 
     { 
      var authAttribute = attribute as AuthorizeAttribute; 
      if (authAttribute != null) 
      { 
       if (!filterContext.HttpContext.User.IsInRole(authAttribute.Roles)) 
       { 
        // User is not authorized so redirect to our access denied error page 
        filterContext.Result = new RedirectToRouteResult(
         new RouteValueDictionary 
          { 
           { "area", "" }, 
           { "controller", "Error" }, 
           { "action", "AccessDenied" } 
          }); 
        break; 
       } 
      } 
     } 
    } 
} 
3

@hellangle और @Andreas ने सुझाव दिया समाधान की तरह, मैं इस समस्या को हल करने के लिए निम्न कोड का प्रयोग किया:

public class CustomizedAuthorizeAttribute : AuthorizeAttribute 
{ 
    public override void OnAuthorization(AuthorizationContext filterContext) 
    { 
     var userAuthInfo = GetUserAuthInfo(); 

     if (!userAuthInfo.IsAuthenticated()) 
     { 
      filterContext.Result = new RedirectResult(UrlToYourLoginPage); 
      return; 
     } 

     if (!userAuthInfo.IsAuthorized()) 
     { 
      var result = new ViewResult {ViewName = "UnAuthorized"}; 
      result.ViewBag.Message = "Sorry! You are not authorized to do this!"; 
      filterContext.Result = result; 
     } 
    } 
} 
बेशक

, आप उपयोगकर्ता प्राधिकरण जानकारी वर्ग और संबंधित तरीकों को लागू करने की जरूरत है (GetUserAuthInfo, IsAuthenticated, IsAuthorized) आपकी विशिष्ट आवश्यकताओं के अनुसार। एमवीसी इंजन कहीं भी कहीं भी 'अनधिकृत' नामक एक दृश्य रखा जाना चाहिए। तो यह एक नियंत्रक वर्ग (@ hellangle के जवाब में बताया) या एक कार्रवाई पद्धति पर इस्तेमाल किया जा सकता:

[CustomizedAuthorizeAttribute] 
public class TargetController : Controller 
{ 
    [CustomizedAuthorizeAttribute] 
    public ActionResult TargetAction() 
    { 
     // Your Code 
    } 

} 

आदेश विभिन्न नियंत्रक वर्गों और कार्रवाई के तरीकों के लिए विभिन्न पहुँच नियंत्रण रणनीति प्रदान करने के लिए CustomizedAuthorizeAttribute के लिए एक निर्माता को लागू करता है कक्षा जो अभिगम नियंत्रण जानकारी का प्रतिनिधित्व करने वाले पैरामीटर को स्वीकार करती है और फिर तदनुसार अनुकूलित प्राधिकृत एट्रिब्यूट क्लास को तत्काल प्रदान करती है।

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