2013-05-16 13 views
7

Z jakiegoś powodu, właściwość UserData mojego pliku cookie uwierzytelniania jest pusta. Oto kod:Właściwość UserData formularza FormsAuthenticationTicket jest pusta pomimo ustawienia

var authCookie = FormsAuthentication.GetAuthCookie(userName, rememberUser.Checked); 
// Get the FormsAuthenticationTicket out of the encrypted cookie 
var ticket = FormsAuthentication.Decrypt(authCookie.Value); 
// Create a new FormsAuthenticationTicket that includes our custom User Data 
var newTicket = new FormsAuthenticationTicket(ticket.Version, ticket.Name, ticket.IssueDate, ticket.Expiration, ticket.IsPersistent, "userData"); 
// Update the authCookie's Value to use the encrypted version of newTicket 
authCookie.Value = FormsAuthentication.Encrypt(newTicket); 
// Manually add the authCookie to the Cookies collection 
Response.Cookies.Add(authCookie); 
FormsAuthentication.RedirectFromLoginPage(userName, rememberUser.Checked); 

tutaj jest jak próbuję uzyskać do niego dostęp:

if (HttpContext.Current.Request.IsAuthenticated) 
{ 
    var authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName]; 
    if (authCookie != null) 
    { 
     var authTicket = FormsAuthentication.Decrypt(authCookie.Value); 
     string data = authTicket.UserData; 
     // data is empty !!! 
    } 
} 

Odpowiedz

7

RedictFromLoginPage nadpisuje plik cookie. Usuń tę linię i przekieruj ręcznie (Response.Redirect).

+0

Używam 'Response.Redirect' ale nadal' UserData' dostał pusty ciąg – Ravi

+0

@Ravi: opublikuj swój fragment kodu jako pytanie. –

+0

wysłałem moje pytanie [http://stackoverflow.com/questions/20816425/getting-formsauthentication-ticket-userdata-as-empty-string](http://stackoverflow.com/questions/20816425/getting-formsauthentication- ticket-userdata-as-empty-string) – Ravi

3

To jest podobna odpowiedź, na którą odpowiedziałem kilka dni temu.

https://stackoverflow.com/a/16365000/296861

Nie można używać FormsAuthentication.SetAuthCookie lub FormsAuthentication.RedirectFromLoginPage jeśli tworzysz FormsAuthenticationTicket samemu.

+0

Uświadomiłem sobie, że FormsAuthentication.GetRedirectUrl również ma ten problem. Pobieranie adresu URL i zapisywanie w zmiennej, a następnie ustawienie pliku cookie i na końcu przekierowanie rozwiązało problem. –

0

wydaje mi robisz ten sam poradnik ... mam napotkał ten sam problem w moim przypadku .. to był błąd web config ..

<authentication mode="Forms"> 
    <forms slidingExpiration="true" timeout="60" cookieless="UseUri"/> 
    </authentication> 

jeśli masz plików cookie = "UseUri" w Twojej konfiguracji sieciowej usuń go .. działa w moim przypadku

Powiązane problemy