My web application's home page has a RememberMe checkbox. If the user checks it, I willl store email-id and password in cookies. This is my code:
if (this.ChkRememberme != null && this.ChkRememberme.Checked == true)
{
HttpCookie cookie = new HttpCookie(TxtUserName.Text, TxtPassword.Text);
cookie.Expires.AddYears(1);
Response.Cookies.Add(cookie);
}
What I want to know is:
- Is it secure to store passwords in cookies?
- What is proper way of doing the same?
- What are the best practices in setting time for a cookie?
It's NOT secure to store passwords in cookies because they are available as plain text.
A good place to find some answers about cookies is Cookie Central. For membership usually is used a cookie with a long string called 'token' that is issued from the website when you provide your user name and password. More about the process you can find in this article. When using forms authentication in ASP.NET you can set the authentication cookie like this:
The second parameter is used for "Remember Me" functionality - if true it will create persistent cookies that will last after you leave the site. You can also programatically manipulate the cookie like this:
No! Don't store passwords in cookies!
In ASP.NET, use
The second argument's value determines if the cookie is persistent (the remember me checkbox's value).
This is what you should never do, because it is very easy to change the value of a cookie and send back to server. Even storing "user is looged in as 'naivists'" in a cookie is wrong, because I could then change it to "user is logged in as 'Pandiya Chendur'".
What you can do in cookies is give information to clients that, even if changed, does not make sense to the server. For instance - favourite color, first page layout et cetera.
You may give them session ID which is stored in a cookie, because they cannot make anything better for themselves, if they change the value to something else (unless they know a valid session ID from another session).
What Microsoft's MSDN says about using cookies:
It is not at all secure. Cookies are stored in the client computer, which can be tampered with.
What Branislav said, and...
In addition to not putting sensitive data in your cookies, you should also secure them by placing at least the following in your web.config:
For more details see: How exactly do you configure httpOnlyCookies in ASP.NET?
If you use SSL which you should if you are transmitting any secure information, that eliminates a third party from listening to your web traffic. This would be the same issue regardless of storing a users credentials in a cookie because when they login your sending their username and password to the server anyway, where I assume the server hashes it and compares it against the hashed password you have for that user.
Other domains will never be able to read your cookie because of cross-origin so that's not an issue.
So really the only "security hole" if you want to call it that is if someone physically gains access to their computer. If that happens they're most likely going to get any information that want from that person anyway. How do you explain when chrome auto fills out login forms for you, is that secure? I'm sure they are not storing it in plain text but that doesn't even matter. If you go to a page that chrome auto fills you can just copy the password out of the form and look at that you now have that persons password.
It really comes down to how "secure" you need it to be. I agree that encrypting a users information with an expiration as a token is the best way to authenticate service calls and it provides flexibility. I just do not see the issue with storing login credentials in a cookie.