cookies怎么保存用户名和密码
cookies怎么保存用户名和密码
然后怎么提取用户名
然后下次登录的时候 如果cookies还在就直接进去了
怎么做啊
C# code
UserInfo user = new UserInfo();
user.username = this.username.Text.ToString();
user.password = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password.Text, "MD5"); //TextBox1.Text为密码框的值
if (user.CheckLogin())
{
//这里怎么用cookies保存用户名和密码 啊。然后 以后提取用户名和密码 怎么用啊 }
else
{
Response.Write("<script>alert('登录失败!')</script>");
}
------解决方案--------------------
一、写Cookies的方法:
Response.cookies["mycookie"]["username"]="admin"
Response.cookies["mycookie"]["password"]="123"
其中“mycookie”就是Cookies名,而后面括号中的就是key
二、读取Cookies的方法:
username=Request.Cookies["mycookie"]["username"]
password= Request.Cookies["mycookie"]["password"]
这只是个临时用法,不能设置保存时间,设置保存时间的要麻烦一点
------解决方案--------------------
System.Web.HttpCookie newcookie = new HttpCookie("user");
newcookie.Values["username"] = "";
newcookie.Values["password"] = "";
newcookie.Expires = DateTime.Now.AddDays(15);
Response.AppendCookie(newcookie);
System.Web.HttpCookie cookie = Request.Cookies["user"];
if (cookie != null)
{
cookie.Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Set(cookie);
}
HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies[cookiename];
if (cookie != null)
{
cookie.Values.Clear();
SetUserCookieExpireTime(cookiename, -1);
cookie.Domain = _domain;
System.Web.HttpContext.Current.Response.Cookies.Set(cookie);
}
public static void SetUserCookieExpireTime(string key, int days)
{
System.Web.HttpContext.Current.Response.Cookies[key].Domain = _domain;
System.Web.HttpContext.Current.Response.Cookies[key].Path = _cookiepath;
System.Web.HttpContext.Current.Response.Cookies[key].Expires = DateTime.Now.AddDays(days);
}