2011-08-05 6 views
0

내가 역할로 설정하고있는 홈 컨트롤러에서 MVC 3에서 권한 부여를 정의하려고 ...사용자 정의 권한을 부여 mvc3 오류

Session["role"] = "Admin"; 

내가

SiteRoles role = (SiteRoles)httpContext.Session["role"]; 
에서 오류가 발생하고

지정 된 캐스트가 유효하지 않습니다.

실마리가 없습니다.

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

    string[] users = Users.Split(','); 

    if (!httpContext.User.Identity.IsAuthenticated) 
     return false; 

    string role = (string)httpContext.Session["role"]; 

    if (Roles != 0 && ((Roles & role) != role)) 
     return false; 

    return true; 
} 
+0

나는 또한 다음 예제에서 ... http : //mvchosting.asphostcentral.com/post/ASPNET-MVC-Hosting-Working-with-Custom-Authorisation-in-ASPNET-MVC-Framework.aspx – Beginner

답변

0

당신은 세션 내부 문자열을 설정하는, 그래서 다시 읽을 때 문자열을 사용한다 :

Session["role"] = SiteRoles.Admin; 

: 일부 사용자 정의 형식을 설정하기를 원한다면

string role = (string)httpContext.Session["role"]; 

또는 그러면 다음을 할 수 있습니다.

SiteRoles role = (SiteRoles)httpContext.Session["role"]; 
+0

감사합니다! 그것은 바보 같았습니다 – Beginner

+0

@Beginner,'SiteRoles'는'Session [ "role"] = SiteRoles.Admin;'을 쓸 때 이미 세션 내부의 정수를 설정하고있는 정수로 구워지는 열거 형인 것처럼 보입니다. 그러나 당신은 또한 Session [ "role"] = 1; –

관련 문제