了解ASP.NET的COOKIES自动登陆的进来看一下
if (Request.Cookies["username"] != null && Request.Cookies["password"] != null)
{
//用户曾登录
username = Request.Cookies["username"].Value.ToString(); //读取Cookie
password = Request.Cookies["password"].Value.ToString();//判断Cookie读取出来的用户名和密码是否能正确登录
if (UserManager.LoginValidate(username, password))
{
//登录后的代码
} }
上边的代码理论上是实现自动登陆的,但是实际操作起来总是出错,意思是读取COOKIES的对象没有实例化,怎么解决?谢谢
username = Request.Cookies["虚销username"].Value.ToString(); //读取Cookie
去掉Value 没Value的 设置了这个悔纯Value 那就是空NULL
在写入的时候
Response.Cookies["userName"] = "Tom";
而不会Response.Cookies["userName"碧誉咐].Value= "Tom";
如果没有登录过呢?应该加个else{//跳转至登录页面 进行登录、写入cookie}
ASP.NET中的cookie:创建搜闷芹Cookie方法 (1)
Response.Cookies["userName"].Value = “admin";
Response.Cookies[“userName”].Expires = DateTime.Now.AddDays(1);
//如果不设置失效时间,Cookie信息不会写到罩游用户硬盘,浏览器关闭将会丢弃。
ASP.NET中的cookie:创建Cookie方法 (2)
HttpCookie aCookie = new HttpCookie(“lastVisit”);
//上一次访问时间
aCookie.Value = DateTime.Now.ToString();
aCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(aCookie);
ASP.NET中的cookie:访问Cookie方法(1)
if(Request.Cookies["userName"] != null)
Label1.Text = Server.HtmlEncode(Request.Cookies["userName"].Value);
访问Cookie方法(2)
if(Request.Cookies["userName"] != null) {
HttpCookie aCookie = Request.Cookies["userName"];
Label1.Text = Server.HtmlEncode(aCookie.Value);
}
ASP.NET中的cookie:创建多值Cookie方法 (1)
Response.Cookies["userInfo"]["userName"] = “admin";
Response.Cookies["userInfo"]["lastVisit"] = DateTime.Now.ToString();
Response.Cookies["userInfo"].Expires = DateTime.Now.AddDays(1);
ASP.NET中的cookie:创建多值Cookie方法 (2)
HttpCookie aCookie = new HttpCookie("userInfo");
aCookie.Values["userName"] = “admin";
aCookie.Values["lastVisit"] = DateTime.Now.ToString();
aCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(aCookie);
ASP.NET中的世毕cookie:读取多值Cookie
HttpCookie aCookie = Request.Cookies["userInfo"];
string userName=aCookie.Values[“userName”];
string lastVisit=aCookie.Values[“lastVisit”];
看着实在是没出错
在机器上面运行了一下
没有出项对象没有实例化
你在登录时,有没有写Cookies