天天看点

获取用户登录次数(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);

<DIV id=mainContent><DIV class=forFlow><!--done--><DIV id=topics><DIV class=post><DIV class=clear></DIV><DIV class=postBody><P><SPAN style="COLOR: #000000" mce_style="COLOR: #000000">防止同一账户重复登录</SPAN></P><P><SPAN style="COLOR: #000000" mce_style="COLOR: #000000"></SPAN> </P><P><SPAN style="COLOR: #000000" mce_style="COLOR: #000000">放在登陆成功的地方:<BR> </SPAN><SPAN style="COLOR: #0000ff" mce_style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000" mce_style="COLOR: #000000"> key </SPAN><SPAN style="COLOR: #000000" mce_style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000" mce_style="COLOR: #000000"> TextBox1.Text; </SPAN><SPAN style="COLOR: #008000" mce_style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000" mce_style="COLOR: #008000">用户名文本框设为cache关键字 </SPAN><SPAN style="COLOR: #008000" mce_style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000" mce_style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff" mce_style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000" mce_style="COLOR: #000000"> uer </SPAN><SPAN style="COLOR: #000000" mce_style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000" mce_style="COLOR: #000000"> Convert.ToString(Cache[key]); </SPAN><SPAN style="COLOR: #008000" mce_style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000" mce_style="COLOR: #008000">读取cache中用户相应的值<BR></SPAN><SPAN style="COLOR: #0000ff" mce_style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000" mce_style="COLOR: #000000"> (uer </SPAN><SPAN style="COLOR: #000000" mce_style="COLOR: #000000">==</SPAN><SPAN style="COLOR: #000000" mce_style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff" mce_style="COLOR: #0000ff">null</SPAN><SPAN style="COLOR: #000000" mce_style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000" mce_style="COLOR: #000000">||</SPAN><SPAN style="COLOR: #000000" mce_style="COLOR: #000000"> uer </SPAN><SPAN style="COLOR: #000000" mce_style="COLOR: #000000">==</SPAN><SPAN style="COLOR: #000000" mce_style="COLOR: #000000"> String.Empty)<SPAN style="COLOR: #008000" mce_style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000" mce_style="COLOR: #008000">判断cache中是否有用户的信息,如果没有相关的值,说明用户未登陆</SPAN><BR>{ <BR>  </SPAN><SPAN style="COLOR: #008000" mce_style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000" mce_style="COLOR: #008000">定义cache过期时间 </SPAN><SPAN style="COLOR: #008000" mce_style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000" mce_style="COLOR: #000000">  TimeSpan SessTimeout </SPAN><SPAN style="COLOR: #000000" mce_style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000" mce_style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff" mce_style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000" mce_style="COLOR: #000000"> TimeSpan(</SPAN><SPAN style="COLOR: #800080" mce_style="COLOR: #800080">0</SPAN><SPAN style="COLOR: #000000" mce_style="COLOR: #000000">, </SPAN><SPAN style="COLOR: #800080" mce_style="COLOR: #800080">0</SPAN><SPAN style="COLOR: #000000" mce_style="COLOR: #000000">, System.Web.HttpContext.Current.Session.Timeout, </SPAN><SPAN style="COLOR: #800080" mce_style="COLOR: #800080">0</SPAN><SPAN style="COLOR: #000000" mce_style="COLOR: #000000">, </SPAN><SPAN style="COLOR: #800080" mce_style="COLOR: #800080">0</SPAN><SPAN style="COLOR: #000000" mce_style="COLOR: #000000">);<BR> </SPAN><SPAN style="COLOR: #008000" mce_style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000" mce_style="COLOR: #008000">第一次登陆的时候插入一个用户相关的cache值,</SPAN><SPAN style="COLOR: #008000" mce_style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000" mce_style="COLOR: #000000">HttpContext.Current.Cache.Insert(key, key, </SPAN><SPAN style="COLOR: #0000ff" mce_style="COLOR: #0000ff">null</SPAN><SPAN style="COLOR: #000000" mce_style="COLOR: #000000">, DateTime.MaxValue, SessTimeout, System.Web.Caching.CacheItemPriority.NotRemovable, </SPAN><SPAN style="COLOR: #0000ff" mce_style="COLOR: #0000ff">null</SPAN><SPAN style="COLOR: #000000" mce_style="COLOR: #000000">); <BR>Session[</SPAN><SPAN style="COLOR: #800000" mce_style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000" mce_style="COLOR: #800000">ADMINID</SPAN><SPAN style="COLOR: #800000" mce_style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000" mce_style="COLOR: #000000">] </SPAN><SPAN style="COLOR: #000000" mce_style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000" mce_style="COLOR: #000000"> TextBox1.Text; <BR>Response.Redirect(</SPAN><SPAN style="COLOR: #800000" mce_style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000" mce_style="COLOR: #800000">main.aspx</SPAN><SPAN style="COLOR: #800000" mce_style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000" mce_style="COLOR: #000000">);<BR>}<BR></SPAN><SPAN style="COLOR: #0000ff" mce_style="COLOR: #0000ff">else</SPAN><SPAN style="COLOR: #000000" mce_style="COLOR: #000000"><BR>{ <BR></SPAN><SPAN style="COLOR: #008000" mce_style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000" mce_style="COLOR: #008000">重复登陆 Response.Write("<mce:script type="text/javascript"><!--

alert('您的账号已经登陆!');window.location='login.aspx';

// --></mce:script>");</SPAN><SPAN style="COLOR: #008000" mce_style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000" mce_style="COLOR: #000000">}</SPAN></P></DIV></DIV></DIV></DIV></DIV>