2012-03-01 19 views
6

मैं अपना खुद का कस्टम प्राधिकरण विशेषता बना रहा हूं, AuthorizeCore विधि को ओवरराइड कर रहा हूं और जानना चाहता हूं कि प्राधिकृत विशेषता टैग में पारित भूमिकाओं तक पहुंच बनाना संभव है या नहीं।कस्टम प्राधिकरण विशेषता से भूमिकाएं एक्सेस करना

[CustomAuthorize(Roles = "Administrator, Sales, Entry")] 

इसे यहाँ अंदर से इन तक पहुँचने के लिए संभव है:

protected override bool AuthorizeCore(HttpContextBase httpContext) 
    { 
    } 

मैं तो स्ट्रिंग विभाजित है और एक सरणी बना सकते हैं

तो उदाहरण के लिए मैं इस किया है।

+0

यहाँ पर नज़र http://stackoverflow.com/a/9479442/745331 – Yorgo

उत्तर

9

आप यह this.Roles कर सकते हैं जो एक स्ट्रिंग है जिसे आपको विभाजित करने की आवश्यकता है।

स्रोत कोड स्वतंत्र रूप से उपलब्ध है।

डिफ़ॉल्ट AuthorizeCore कार्यान्वयन:

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

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

    if (_usersSplit.Length > 0 && !_usersSplit.Contains(user.Identity.Name, StringComparer.OrdinalIgnoreCase)) { 
     return false; 
    } 

    if (_rolesSplit.Length > 0 && !_rolesSplit.Any(user.IsInRole)) { 
     return false; 
    } 

    return true; 
} 

और वे एक आंतरिक विभाजन समारोह जो इस तरह दिखता है:

internal static string[] SplitString(string original) { 
    if (String.IsNullOrEmpty(original)) { 
     return new string[0]; 
    } 

    var split = from piece in original.Split(',') 
       let trimmed = piece.Trim() 
       where !String.IsNullOrEmpty(trimmed) 
       select trimmed; 
    return split.ToArray(); 
} 
संबंधित मुद्दे