文档库 最新最全的文档下载
当前位置:文档库 › 使用Cookie完成自动登录

使用Cookie完成自动登录

使用Cookie完成自动登录 - 小小程序员 - JavaEye技术网站首页 新闻 论坛 问答 博客 招聘 更多 ▼
专栏 文摘 圈子 搜索 欢迎wuzongbo_9688 收件箱 我的应用
我的关注 我的参与 我的闲聊 我的圈子 我的收藏 我的简历
我的博客 设置 退出 小小程序员永久域名 https://www.wendangku.net/doc/0213501339.html,2顶
0踩CSS 快速上手(一) | 程序员的十个好习惯 2010-02-01
使用Cookie完成自动登录
文章分类:.net编程
C#代码
//------------------------------登录页面----------------------------
string username = this.txtUserName.Text;//用户名
string password = this.txtPassword.Text;//密码

if (UserManager.LoginValidate(username, password) && this.ckState.Checked ==
true)//判断登录名和密码是否正确和是否选择了记住用户名和密码的复选框
{
//判断客户端浏览器是否存在该Cookie 存在就先清除

if (Request.Cookies["username"] != null && Request.Cookies["password"]
!= null)
{
Response.Cookies["username"].Expires =
System.DateTime.Now.AddSeconds(-1);//Expires过期时间
Response.Cookies["password"].Expires =
System.DateTime.Now.AddSeconds(-1);
}
else
{
//向客户端浏览器加入Cookie (用户名和密码 最好是使用MD5加密)
HttpCookie hcUserName1 = new HttpCookie("username");
hcUserName1.Expires = System.DateTime.Now.AddDays(7);
hcUserName1.Value = username;
HttpCookie hcPassword1 = new HttpCookie("password");
hcPassword1.Expires = System.DateTime.Now.AddDays(7);
hcPassword1.Value = password;
Response.Cookies.Add(hcUserName1);
Response.Cookies.Add(hcPassword1);
}


//------------------------------登录页面----------------------------
string username = this.txtUserName.Text;//用户名
string password = this.txtPassword.Text;//密码

if (UserManager.LoginValidate(username, password) && this.ckState.Checked == true)//判断登录名和密码是否正确和是否选择了记住用户名和密码的复选框
{
//判断客户端浏览器是否存在该Cookie 存在就先清除

if (Request.Cookies["username"] != null && Request.Cookies["password"] != null)
{
Response.Cookies["username"].Expires = System.DateTime.Now.AddSeconds(-1);//Expires过期时间
Response.Cookies["password"].Expires = System.DateTime.Now.AddSeconds(-1);
}
else
{
//向客户端浏览器加入Cookie (用户名和密码 最好是使用MD5加密)
HttpCookie hcUserName1 = new HttpCookie("username");
hcUserName1

.Expires = System.DateTime.Now.AddDays(7);
hcUserName1.Value = username;
HttpCookie hcPassword1 = new HttpCookie("password");
hcPassword1.Expires = System.DateTime.Now.AddDays(7);
hcPassword1.Value = password;
Response.Cookies.Add(hcUserName1);
Response.Cookies.Add(hcPassword1);
}
}
C#代码
//------------------------转到登录后的页面(有母板页写母板页最好)------------------------
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))
{
//登录后的代码
} }
//------------------------转到登录后的页面(有母板页写母板页最好)------------------------
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))
{
//登录后的代码
} }C#代码
//-------------------------------Cookie退出(加一个退出按钮)-------------------------------

//-------------------------------Cookie退出(加一个退出按钮)-------------------------------C#代码
//退出
HttpCookie hcUserName1 = new HttpCookie("username");
hcUserName1.Expires = System.DateTime.Now.AddDays(-7);
hcUserName1.Value = username;
HttpCookie hcPassword1 = new HttpCookie("password");
hcPassword1.Expires = System.DateTime.Now.AddDays(-7);
hcPassword1.Value = password;
Response.Cookies.Add(hcUserName1);
Response.Cookies.Add(hcPassword1);
//退出
HttpCookie hcUserName1 = new HttpCookie("username");
hcUserName1.Expires = System.DateTime.Now.AddDays(-7);
hcUserName1.Value = username;
HttpCookie hcPassword1 = new HttpCookie("password");
hcPassword1.Expires = System.DateTime.Now.AddDays(-7);
hcPassword1.Value = password;
Response.Cookies.Add(hcUserName1);
Response.Cookies.Add(hcPassword1);
2


顶0
踩CSS 快速上手(一) | 程序员的十个好习惯 12:57浏览 (145)评论 (0)分类: .NET收藏相关推荐评论发表评论
完成论坛规则小测验以后,您才能在论坛发帖和回复。
lyqidao

开始学习Java SSH
2010-02-05 通过网页
>>更多闲聊浏览: 5851 次
性别:
来自: 广州

详细资料 留言簿
发短消息 关注
搜索本博客

最近访客 >>更多访客
咖啡仔cllian119lenvessmoke_cloud_powder博客分类
全部博客 (29)
.NET (6)
JAVA (9)
数据库 (0)
HTML/CSS (1)
JAVASCRIPT (0)
AJAX (0)
WEB (0)
转载于我的其他博客 (1)
转载与他人 (6)
我的随笔 (5)
项目实战 (0)
我的相册
截图2
共 5 张 我的留言簿 >>更多留言
我的MSN : liuyang0324@https://www.wendangku.net/doc/0213501339.html,
-- by lyqidao
你好阳光阿
-- by lyqidao
谢谢了
-- by zhuzhiguosnail
其他分类
我的收藏 (1)
我的书籍 (1)
我的论坛主题贴 (29)
我的所有论坛贴 (4)
我的精华良好贴 (0)
最近加入圈子
https://www.wendangku.net/doc/0213501339.html,之C#网站开发
存档
2010-03 (1)
2010-02 (28)
更多存档...
最新评论
TomCat环境变量配置
longforfreedom 写道用IDE的话,CATALINA_HOME可能不用 ...
-- by lyqidao
TomCat环境变量配置
用IDE的话,CATALINA_HOME可能不用配置。在IDE里添加就可以了。
-- by longforfreedom
JSP万能数据库连接
呵呵,实际项目中你可能很少写这样的代码 。
-- by longforfreedom
程序员的十个好习惯
linjuan8719 写道受益匪浅,很有道理! 一起学习
-- by lyqidao
程序员的十个好习惯
nenyalanye_1 写道写的不错。。谨记下来谢谢,一起学习
-- by lyqidao
评论排行榜
程序员的十个好习惯
好书不断推(职场、商战、技术)
TomCat环境变量配置
JSP万能数据库连接
为什么要选择JavaEye?



[什么是RSS?]


声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。若作者同意转载,必须以超链接形式标明文章原始出处和作者。
? 2003-2009 https://www.wendangku.net/doc/0213501339.html,. All rights reserved. 上海炯耐计算机软件有限公司 [ 沪ICP备05023328号 ]

相关文档
相关文档 最新文档