Link to home
Start Free TrialLog in
Avatar of orbulat
orbulatFlag for Hong Kong

asked on

FormsAuthenticationTicket userdata empty

hi all, i am using FormsAuthenticationTicket  
add the cookie in login.aspx

but when i want to retrieve the cookie from default.aspx
the userdata is empty

but the userdata appears again when i close my browser and open it again

any idea on this?

thanks a lot!
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, "myname",
         DateTime.Now, DateTime.Now.AddMinutes(dCookieLife), true, "my_role", FormsAuthentication.FormsCookiePath);
        string ticketEncrypted = FormsAuthentication.Encrypt(ticket);
        HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, ticketEncrypted);
        authCookie.HttpOnly = true;
        authCookie.Path = FormsAuthentication.FormsCookiePath;
        authCookie.Domain = myHKJCCookieDomainName;
        authCookie.Expires = ticket.Expiration;
        HttpContext.Current.Response.Cookies.Add(authCookie););        
         Response.Redirect("default.aspx");   
        }
 
 
in default.aspx
 
if (Request.Cookies[".ASPXAUTH"] != null)
        {
            try
            {
                FormsAuthenticationTicket ticket1 = FormsAuthentication.Decrypt(Request.Cookies[".ASPXAUTH"].Value);
                Response.Write("UserData: " + ticket1.UserData+ "<br>");
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }

Open in new window

Avatar of nkiran4u
nkiran4u
Flag of India image

i suggest u to use sessions instead of cookies(since session will not expire until the browser window is closed)......

at the same time write the request.cookies in  IfNotpostback  function

if possible post ur entire code and ill try to help u....... also do u just want to display the username in remaining pages when the user is logged in (or) do u want to retreive the saved password(ie if u have checked remember me option wile loggin in (or) do u want to check the encrypted password of the user with the database....
Avatar of orbulat

ASKER

i can't use session my case

in fact i want to retrieve the userData which holds the role

it's weird that,
1. clear cookie
2. go to logon page (form cookie is written here)
3. user is authenticated
4. redirect to default.aspx (using response.redirect)
5. userdata can't be retrieved, it's empty
6. close browser
7. open browser, go to default.aspx
8. userdata can be retrieved

the above code is for testing to see why userdata can't be retrieved immediately after writing in logon page
Avatar of orbulat

ASKER

and i just looked at the ticket name

first time login

FormsAuthenticationTicket ticket1 = FormsAuthentication.Decrypt(Request.Cookies[".ASPXAUTH"].Value);

ticket1.Name --> it's sth. like guid

but when i close browser and logon again
the ticket name becomes "myname"

any ideas?

thanks!
ASKER CERTIFIED SOLUTION
Avatar of orbulat
orbulat
Flag of Hong Kong image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial