문제

을 구현하려는 사용자 정의 AuthorizeAttribute.

나는 사용자 지정:

    public class MyUser : IPrincipal
    {
        public List<string> Roles { get; set; }
        public IIdentity Identity{ get; set; } 
        public bool IsInRole(string role){ return Roles.Contains(role); }
    }

global.asax

    protected void Session_Start(object sender, EventArgs e)
    {
         MyUser user = new MyUser();
         user.Identity = User.Identity;
         user.Roles = new List<string>();
         user.Roles.Add("MyRole");//I will get them from AD
         HttpContext.Current.User = user;
    }

고 특성

    public class AuthorizeADAttribute : AuthorizeAttribute
    {
        public string Roles { get; set; }
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            MyUser user = (MyUser)httpContext.User;//User is not MyUser
            if (user.Roles.Contains(Roles))
            {
                return true;
            }
            return false;
        }

        protected override void HandleUnauthorizedRequest(
        AuthorizationContext filterContext)
        {
            ...
        }
    }

문제는 httpContext.UserSystem.Security.Claims.ClaimsPrincipal.왜 일어나는 방법에 액세스할 수 있습니까 내 사용자에서 session_start ?

도움이 되었습니까?

해결책

당신이 필요하는 덮어쓰기에서 그 가치 Application_PostAuthenticateRequest 이벤트 처리기에 Global.asax.cs.

이 대답 예를 들어: https://stackoverflow.com/a/10524305/54937

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top