I’m writing a database authentication system for my web application which is wrriten in ASP.NET MVC. When someone authorize, it should save his username in cookies. Is it safe to just use HttpResponse.Cookies` for saving a cookie that its value is username? Wouldn’t it forgeable?
Saving only the username… Is it the right and safe way? Or should I save the whole User object (if this actually possible)?
Thank you and sorry for my English.
No, saving the username is very insecure, because it can be easily faked. Here are my guidelines:
Tokens can be generated by a CSPRNG to ensure that the auto-login cookie can not be faked.
Hashing the tokens in the database prevents user account stealing in the case that you database is compromised. (Remember, the token at this point is a password-equivalent.)
HTTP-Only cookies prevent XSS attacks that could potentially steal the cookie.