2013-05-03 2 views
1

Im는 asp.net MVC 4 응용 프로그램에서 FormsAuthentication을 사용하려고합니다. 인증 자체가 잘 작동하지만, Im이 FormsAuthenticationEventArgs.User에서 사용자를 가져 와서 Http.Context.Current.User에 할당하면 Http.Context.Current.User가 Null입니다. . 내가 뭘 잘못하고 있니? 사용자 지정 IPrincipal 개체에 현재 HttpContext에의 사용자 속성을 설정할 수 있도록Asp.net MVC 4 및 FormsAuthentication.User

protected void FormsAuthentication_OnAuthenticate(Object sender, FormsAuthenticationEventArgs e) 
    { 

     if (FormsAuthentication.CookiesSupported == true) 
     { 
      if (Request.Cookies[FormsAuthentication.FormsCookieName] != null) 
      { 
       try 
       { 
        string username = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name; 
        var usuarioRn = new UsuarioRN(); 
        string roles = usuarioRn.GetRoles(username); 

        FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value); 

        e.User = new System.Security.Principal.GenericPrincipal(new System.Web.Security.FormsIdentity(ticket), roles.Split(';')); 
        //First time get here assign e.User to HttpContext.Current.User, all good 
        //Next call HttpContext.Current.User is again null, why? 
        HttpContext.Current.User = e.User;      
       } 
       catch (Exception) 
       { 

       } 
      } 
     } 
+0

왜 사용자에게이 방법을 지정 하시겠습니까? FormsAuthentication이없이 HttpContext.Current.User를 채 웁니다. 또 다른 메모 - MVC의 Context.User는 HttpContext.Current.User와 같지 않습니다. 사용자에게 전화를 걸 때 코드를 게시 할 수 있습니까? – gabnaim

+0

나는 지금 당장 운전하고있다. 나는 코드를 게시 할 것이다. –

+0

@ ramires.cabral - FormsAuthentication_OnAuthenticate 대신 Application_AuthenticateRequest를 사용해야합니다. –

답변

0

FormsAuthenticationEventArgs.User는 것입니다.

는이 서명 한 경우는 아마 훨씬 쉬울 것

이해하기 : 즉

FormsAuthenticationEventArgs.SetHttpContextUser(IPrinciple user); 

을 항상 null이 될 것이다 - 당신이 뭔가를 할당 할 때 실제로 뭔가 다른 (HttpContext를 사용)을 설정 그것에.