獲取使用者登入次數(cookie)

weixin_33766168發表於2017-11-15

登入的時候先取cookie,取到就加1.然後儲存。

 

1
2
3
4
5
6
7
8
9
10
11
12
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();
}

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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>");
}


    本文轉自曾祥展部落格園部落格,原文連結:http://www.cnblogs.com/zengxiangzhan/archive/2009/12/23/1630870.html,如需轉載請自行聯絡原作者


相關文章