2012-03-01 8 views
6

AuthorizeCore 메서드를 재정의하고 authorize 특성 태그에 전달 된 역할에 액세스 할 수 있는지 알고 싶다고 직접 정의한 사용자 지정 특성을 만들었습니다.사용자 지정 권한 부여 특성에서 역할에 액세스

[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(); 
} 
관련 문제