代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Asp.Net
】
获取用户登录次数(cookie)
作者:
Dezai.CN
/ 发布于
2012/7/26
/
630
获取用户登录次数 登录的时候先取cookie,取到就加1.然后保存
<div> <div class="container"> <div class="line number1 index0 alt2">"c# keyword">if "c# plain">(Request.Cookies["c# string">"loginCount""c# plain">] == "c# keyword">null"c# plain">)</div> <div class="line number2 index1 alt1">"c# plain">{</div> <div class="line number3 index2 alt2">"c# plain">HttpCookie c= "c# keyword">new "c# plain">HttpCookie("c# string">"loginCount""c# plain">); ;</div> <div class="line number4 index3 alt1">"c# plain">Response.Cookies["c# string">"loginCount""c# plain">].Value = "c# string">"1""c# plain">;</div> <div class="line number5 index4 alt2">"c# plain">Response.Cookies["c# string">"loginCount""c# plain">].Expires = DateTime.Now.AddDays(1);</div> <div class="line number6 index5 alt1">"c# plain">Response.Cookies.Add(c);</div> <div class="line number7 index6 alt2">"c# plain">}</div> <div class="line number8 index7 alt1">"c# keyword">else</div> <div class="line number9 index8 alt2">"c# plain">{</div> <div class="line number10 index9 alt1">"c# keyword">int "c# plain">count = Convert.ToInt32(Request.Cookies["c# string">"loginCount""c# plain">].Value) + 1;</div> <div class="line number11 index10 alt2">"c# plain">Response.Cookies["c# string">"loginCount""c# plain">].Value = count.ToString();</div> <div class="line number12 index11 alt1">"c# plain">}</div> <div class="line number12 index11 alt1"></div> <div class="line number12 index11 alt1"></div> <div class="line number12 index11 alt1"></div> <div class="line number12 index11 alt1"></div> <div class="line number12 index11 alt1">"c# plain"> <div class="container"> <div class="line number1 index0 alt2">"c# keyword">if "c# plain">(Request.Cookies["c# string">"userCookie""c# plain">] == "c# keyword">null"c# plain">)</div> <div class="line number2 index1 alt1">"c# plain">{</div> <div class="line number3 index2 alt2">"c# plain">HttpCookie userCookie = "c# keyword">new "c# plain">HttpCookie("c# string">"userCookie""c# plain">);</div> <div class="line number4 index3 alt1">"c# plain">userCookie.Values["c# string">"userName""c# plain">] = userInfo.UserName.ToString();</div> <div class="line number5 index4 alt2">"c# plain">userCookie.Values["c# string">"lastVist""c# plain">] = DateTime.Now.ToString();</div> <div class="line number6 index5 alt1">"c# plain">userCookie.Values["c# string">"count""c# plain">] = "c# string">"1""c# plain">;</div> <div class="line number7 index6 alt2">"c# plain">userCookie.Expires = DateTime.Now.AddDays(30);</div> <div class="line number8 index7 alt1">"c# plain">Response.Cookies.Add(userCookie);</div> <div class="line number9 index8 alt2">"c# plain">}</div> <div class="line number10 index9 alt1">"c# keyword">else</div> <div class="line number11 index10 alt2">"c# plain">{</div> <div class="line number12 index11 alt1">"c# keyword">int "c# plain">counter = Convert.ToInt32(Request.Cookies["c# string">"userCookie""c# plain">]["c# string">"count""c# plain">]) + 1;</div> <div class="line number13 index12 alt2">"c# plain">HttpCookie userCookie = "c# keyword">new "c# plain">HttpCookie("c# string">"userCookie""c# plain">);</div> <div class="line number14 index13 alt1">"c# plain">userCookie.Values["c# string">"userName""c# plain">] = userInfo.UserName.ToString();</div> <div class="line number15 index14 alt2">"c# plain">userCookie.Values["c# string">"lastVist""c# plain">] = DateTime.Now.ToString();</div> <div class="line number16 index15 alt1">"c# plain">userCookie.Values["c# string">"count""c# plain">] = counter.ToString();</div> <div class="line number17 index16 alt2">"c# plain">userCookie.Expires = DateTime.Now.AddDays(30);</div> <div class="line number18 index17 alt1">"c# plain">Response.Cookies.Add(userCookie);</div> <div class="line number19 index18 alt2">"c# plain">}</div> <div class="line number20 index19 alt1">"c# plain">在另一个页面取出来</div> <div class="line number21 index20 alt2">"c# comments">//读取Cookie</div> <div class="line number22 index21 alt1">"c# keyword">string "c# plain">nameCookie = Request.Cookies["c# string">"userCookie""c# plain">]["c# string">"userName""c# plain">];</div> <div class="line number23 index22 alt2">"c# plain">Response.Write("c# string">"用户名:" "c# plain">+ nameCookie);</div> <div class="line number24 index23 alt1">"c# keyword">string "c# plain">timeCookie = Request.Cookies["c# string">"userCookie""c# plain">]["c# string">"lastVist""c# plain">];</div> <div class="line number25 index24 alt2">"c# plain">Response.Write("c# string">" 上传访问时间:" "c# plain">+ timeCookie);</div> <div class="line number26 index25 alt1">"c# keyword">string "c# plain">countCookie = Request.Cookies["c# string">"userCookie""c# plain">]["c# string">"count""c# plain">];</div> <div class="line number27 index26 alt2">"c# plain">Response.Write("c# string">" 访问次数:" "c# plain">+ countCookie);</div> </div> </div> <div class="line number12 index11 alt1"></div> <div class="line number12 index11 alt1"></div> <div class="line number12 index11 alt1"></div> <div class="line number12 index11 alt1"></div> <div class="line number12 index11 alt1">"c# plain"> <span style="color: rgb(0, 0, 0);">防止同一账户重复登录</span> <span style="color: rgb(0, 0, 0);">放在登陆成功的地方: </span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);"> key </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> TextBox1.Text; </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">用户名文本框设为cache关键字 </span><span style="color: rgb(0, 128, 0);"> </span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);"> uer </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> Convert.ToString(Cache[key]); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">读取cache中用户相应的值 </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (uer </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">||</span><span style="color: rgb(0, 0, 0);"> uer </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> String.Empty)<span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">判断cache中是否有用户的信息,如果没有相关的值,说明用户未登陆</span> { </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">定义cache过期时间 </span><span style="color: rgb(0, 128, 0);"> </span><span style="color: rgb(0, 0, 0);"> TimeSpan SessTimeout </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> TimeSpan(</span><span style="color: rgb(128, 0, 128);">0</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 128);">0</span><span style="color: rgb(0, 0, 0);">, System.Web.HttpContext.Current.Session.Timeout, </span><span style="color: rgb(128, 0, 128);">0</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 128);">0</span><span style="color: rgb(0, 0, 0);">); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">第一次登陆的时候插入一个用户相关的cache值,</span><span style="color: rgb(0, 128, 0);"> </span><span style="color: rgb(0, 0, 0);">HttpContext.Current.Cache.Insert(key, key, </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">, DateTime.MaxValue, SessTimeout, System.Web.Caching.CacheItemPriority.NotRemovable, </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">); Session[</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">ADMINID</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> TextBox1.Text; Response.Redirect(</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">main.aspx</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">); } </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"> { </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">重复登陆 Response.Write("<script>alert('您的账号已经登陆!');window.location='login.aspx';</script>");</span><span style="color: rgb(0, 128, 0);"> </span><span style="color: rgb(0, 0, 0);">}</span> </div> </div> </div>
试试其它关键字
防止同一账户重复登录
同语言下
.
gzip压缩
.
实现http多线程断点续传下载文件
.
实现多线程断点续传下载大文件
.
生成字符串的 CheckSum
.
根据 UserAgent 获取浏览器的类型和版本
.
根据 Agent 判断是否是智能手机
.
隐藏手机号中间四位为*方法
.
合并图片(二维码和其他图片合并)
.
ASP.NET CORE中判断是否移动端打开网页
.
ASP.NET(C#)实现页面计时(定时)自动跳转
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3