Instead of directly storing the username and password in a cookie, save the username and password hash and salt in the cookie, then when you authenticate the cookie, get the password for that username, re-create the hash with the same password salt and compare them.
, , , ( MD5 , ) (, base64).
:
public string CreateHash(string password, string salt)
{
string authDetails = password + salt;
byte[] authBytes = System.Text.Encoding.ASCII.GetBytes(authDetails);
var md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] hashedBytes = md5.ComputeHash(authBytes);
string hash = Convert.ToBase64String(hashedBytes);
return hash;
}
public bool IsMatchingHash(string password, string salt, string hash)
{
return CreateHash(password, salt) == hash;
}
public HttpCookie CreateAuthCookie(string username, string password, string salt)
{
HttpCookie cookie = new HttpCookie("YourSiteCookieNameHere");
cookie.Value = username + "|" + CreateHash(password, salt);
return cookie;
}
public bool IsValidAuthCookie(HttpCookie cookie, string salt)
{
string[] values = cookie.Value.Split('|');
if (values.Length != 2) return false;
string username = values[0];
string hash = values[1];
string password = GetPasswordForUser(username);
return IsMatchingHash(password, salt, hash);
}