获取用户登录次数(cookie)

登录的时候先取cookie,取到就加1.然后保存。

 

if (Request.Cookies["loginCount"] == null)
{
HttpCookie c= new HttpCookie("loginCount"); ;
Response.Cookies["loginCount"].Value = "1";
Response.Cookies["loginCount"].Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(c);
}
else
{
int count = Convert.ToInt32(Request.Cookies["loginCount"].Value) + 1;
Response.Cookies["loginCount"].Value = count.ToString();
}

 

 

 

 if (Request.Cookies["userCookie"] == null)
{
HttpCookie userCookie = new HttpCookie("userCookie");
userCookie.Values["userName"] = userInfo.UserName.ToString();
userCookie.Values["lastVist"] = DateTime.Now.ToString();
userCookie.Values["count"] = "1";
userCookie.Expires = DateTime.Now.AddDays(30);
Response.Cookies.Add(userCookie);
}
else
{
int counter = Convert.ToInt32(Request.Cookies["userCookie"]["count"]) + 1;
HttpCookie userCookie = new HttpCookie("userCookie");
userCookie.Values["userName"] = userInfo.UserName.ToString();
userCookie.Values["lastVist"] = DateTime.Now.ToString();
userCookie.Values["count"] = counter.ToString();
userCookie.Expires = DateTime.Now.AddDays(30);
Response.Cookies.Add(userCookie);
}
在另一个页面取出来
//读取Cookie
string nameCookie = Request.Cookies["userCookie"]["userName"];
Response.Write("用户名:" + nameCookie);
string timeCookie = Request.Cookies["userCookie"]["lastVist"];
Response.Write(" <br>上传访问时间:" + timeCookie);
string countCookie = Request.Cookies["userCookie"]["count"];
Response.Write(" <br>访问次数:" + countCookie); 

防止同一账户重复登录

 

放在登陆成功的地方:
 
string key = TextBox1.Text; //用户名文本框设为cache关键字 
 string uer = Convert.ToString(Cache[key]); //读取cache中用户相应的值
if (uer == null || uer == String.Empty)//判断cache中是否有用户的信息,如果没有相关的值,说明用户未登陆
{ 
  
//定义cache过期时间 
  TimeSpan SessTimeout = new TimeSpan(00, System.Web.HttpContext.Current.Session.Timeout, 00);
//第一次登陆的时候插入一个用户相关的cache值,
HttpContext.Current.Cache.Insert(key, key, null, DateTime.MaxValue, SessTimeout, System.Web.Caching.CacheItemPriority.NotRemovable, null); 
Session[
"ADMINID"= TextBox1.Text; 
Response.Redirect(
"main.aspx");
}
else
{ 
//重复登陆 Response.Write("<script>alert('您的账号已经登陆!');window.location='login.aspx';</script>");
}

posted @ 2009-12-23 18:52  曾祥展  阅读(4382)  评论(2编辑  收藏  举报