AuthorizeAttribute
के मेरे संशोधित कार्यान्वयन से कोड यहां दिया गया है; मैंने इसे SecurityAttribute
नाम दिया। केवल एक चीज है कि मैं बदल गया है OnAuthorization
तरीका है, और URL अनधिकृत पृष्ठ पर रीडायरेक्ट करने के लिए मैं एक अतिरिक्त स्ट्रिंग संपत्ति कहा: कि वहाँ छोड़कर,
// Set default Unauthorized Page Url here
private string _notifyUrl = "/Error/Unauthorized";
public string NotifyUrl {
get { return _notifyUrl; } set { _notifyUrl = value; }
}
public override void OnAuthorization(AuthorizationContext filterContext) {
if (filterContext == null) {
throw new ArgumentNullException("filterContext");
}
if (AuthorizeCore(filterContext.HttpContext)) {
HttpCachePolicyBase cachePolicy =
filterContext.HttpContext.Response.Cache;
cachePolicy.SetProxyMaxAge(new TimeSpan(0));
cachePolicy.AddValidationCallback(CacheValidateHandler, null);
}
/// This code added to support custom Unauthorized pages.
else if (filterContext.HttpContext.User.Identity.IsAuthenticated)
{
if (NotifyUrl != null)
filterContext.Result = new RedirectResult(NotifyUrl);
else
// Redirect to Login page.
HandleUnauthorizedRequest(filterContext);
}
/// End of additional code
else
{
// Redirect to Login page.
HandleUnauthorizedRequest(filterContext);
}
}
आप इसे मूल AuthorizeAttribute
के रूप में एक ही तरह से फोन अनधिकृत पृष्ठ Url ओवरराइड करने के लिए एक अतिरिक्त संपत्ति है:
// Use custom Unauthorized page:
[Security (Roles="Admin, User", NotifyUrl="/UnauthorizedPage")]
// Use default Unauthorized page:
[Security (Roles="Admin, User")]
स्रोत
2010-03-24 17:50:50
मैं एक ही सवाल यहाँ से पूछा कि आपका AppSettings प्रविष्टि जोड़ने बनाने: http://stackoverflow.com/questions/2322366/how-do -आई-सर्विस-अप-अनधिकृत-पेज-जब-उपयोगकर्ता-न-इन-द-अधिकृत-भूमिकाएं –
रॉबर्ट: धन्यवाद। मैंने पोस्ट करने से पहले समान पदों की खोज की, लेकिन आपका नहीं देखा। आप किस समाधान के साथ गए थे? – royco
मैंने 'AuthorizeAttribute'' से कोड का उपयोग करके और इसे संशोधित करते हुए अपनी 'AuthorizeRoles' विशेषता रोलिंग समाप्त कर दी। यदि आप कल तक इंतजार कर सकते हैं, तो मैं कोड पोस्ट करूंगा। –