2009-09-20 8 views
1

메신저 내 인증 속성을 사용자 정의하여 내 MemberhipProvider에서 올바르게 구현할 수있는 방법을 찾고 있습니다.사용자 지정 인증 특성 추가 매개 변수?

내가 필요로하는 것은 IsInRoles (문자열 역할, int 파마) 예를 들어, 다른 단어로, 나는 그것을 새로운 IsinRoles로 바꾸거나이 결과를 보관할 다른 방법을 만들고 싶습니다.

가능합니까? 또는 다른 권한 부여 특성을 작성해야합니까? 메신저 ASP.net MVC 작업, 그래서 [권한 부여]를 가지고하고 실행 필터링해야합니다

PS는 ... 걱정 해 주셔서 대단히 감사합니다.

답변

5

사용자 정의 AuthorizeAttribute에 public 속성을 추가 할 수 있다고 생각합니다. 내가 생각

public class CustomAuthorizeAttribute : AuthorizeAttribute 
{ 
    /// <summary> 
    /// Add the allowed roles to this property. 
    /// </summary> 
    public YourCustomRoles RequiredRole; 

    public int YourCustomValue; 

    /// <summary> 
    /// Checks to see if the user is authenticated and has the 
    /// correct role to access a particular view. 
    /// </summary>   
    /// <param name="httpContext"></param> 
    /// <returns></returns> 
    protected override bool AuthorizeCore(HttpContextBase httpContext) 
    { 
     if (httpContext == null) throw new ArgumentNullException("httpContext"); 

     // Make sure the user is authenticated. 
     if (httpContext.User.Identity.IsAuthenticated == false) return false; 

     // Can use your properties if needed and do your checks 
     bool authorized = DoSomeCustomChecksHere(); 

     return authorized; 
    } 
} 

사용법 (하지만 그것을 시도하지 않은) 될 것이다 :

[CustomAuthorizeAttribute (RequiredRole=MyCustomRole.Role1 | MyCustomRole.Role2, YourCustomValue=1234)] 
+0

는 컨트롤러 전에 [Authorize.IsInRole ("XXX")] 필터와 같은 가능한입니까? thnx – DucDigital

+0

고마워, 그냥 내 새로운 권한 부여 클래스에 그것을 구현하고 그것을 컨트롤러 필터에서 호출해야합니다. :) – DucDigital

관련 문제