Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear programmers. I am new to secure programming in ASP.NET. I have one doubt.

What is persistent cookie? And what is the relation between the ticket and cookie? And why some times we will encrypt the cookie.

C#
var ticket = new FormsAuthenticationTicket(txtUsername.Text,true,10);
        var encryptedTicket = FormsAuthentication.Encrypt(ticket);
        var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket)
        {
            HttpOnly = true,
            Secure = FormsAuthentication.RequireSSL,
            Path = FormsAuthentication.FormsCookiePath,
            Domain = FormsAuthentication.CookieDomain
        };
        Response.AppendCookie(cookie);
        Response.Redirect("~/homepage.aspx");



What will be the background processes when i execute the above code.
Posted

The non persistent cookie will live in user browser. the moment user will close the browser the cookie will be deleted. This is the normal way we use with authentication cookies.

persistent cookie on the other hand get saved on the user hard disk. this is typically done when we want to implement "remember me" type of lo-gin functionality. since the cookie will be saved on user computer, next time when the user access the page that cookie will serve as authentication ticket and the user will be logged in.

As for why is it encrypted, if we don't encrypt it then we are potentially sending user credential related sensitive information over the internet. It can be eavesdropped and then user maliciously by someone else (perhaps a hacker or hacker wannabe)

Note: You can refer to following article for details on custom forms authentication: Understanding and Implementing ASP.NET Custom Forms Authentication[^]
 
Share this answer
 
v2
Comments
AshishChaudha 9-Aug-12 8:29am    
my +5
Check this LINK.
 
Share this answer
 
I am using the following code......

C#
tkt = new FormsAuthenticationTicket(1, txtUsername.Text, DateTime.Now,
     DateTime.Now.AddMinutes(30), false, "Under the trees");

           cookiestr = FormsAuthentication.Encrypt(tkt);
           ck = new HttpCookie(FormsAuthentication.FormsCookieName, cookiestr);

           if (tkt.IsPersistent)
           {
               ck.Expires = tkt.Expiration;
           }
           ck.Path = FormsAuthentication.FormsCookiePath;
           Response.Cookies.Add(ck);

           Response.Redirect("index.aspx");
 
Share this answer
 
Comments
Rahul Rajat Singh 9-Aug-12 1:41am    
Why is this posted as solution? This is not a solution. Please use improve question to add such things.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900