2013-05-16 2 views
7

Aus irgendeinem Grund ist die UserData-Eigenschaft meines Authentifizierungscookies leer. Hier ist der Code:Die UserData-Eigenschaft von FormsAuthenticationTicket ist leer, obwohl sie festgelegt wurde

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); 

Hier ist, wie ich versuchen, darauf zuzugreifen:

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 !!! 
    } 
} 

Antwort

7

RedictFromLoginPage überschreibt Cookie. Entfernen Sie diese Zeile und leiten Sie sie manuell um (Response.Redirect).

+0

Ich benutze 'Response.Redirect', aber immer noch' UserData' hat leere Zeichenfolge – Ravi

+0

@Ravi: poste dein Code-Snippet als Frage. –

+0

Ich habe meine Frage gepostet [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

Dies ist eine ähnliche Antwort, die ich vor einigen Tagen geantwortet habe.

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

Sie können nicht FormsAuthentication.SetAuthCookie oder FormsAuthentication.RedirectFromLoginPage verwenden, wenn Sie FormsAuthenticationTicket selbst erstellen.

+0

Ich habe festgestellt, dass auch FormsAuthentication.GetRedirectUrl dieses Problem hat. Das Abrufen der URL und das Speichern in einer Variablen, das Setzen des Cookies und schließlich das Umleiten lösten das Problem. –

0

es mir scheint, dass Sie die gleiche Tutorial taten ... ich habe das gleiche Problem in meinem Fall begegnet .. es ist ein Web-Konfigurationsfehler war ..

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

wenn Sie cookieless = "UseUri" in Ihrer Web-Konfiguration entfernen Sie es .. es funktioniert in meinem Fall

Verwandte Themen