cookie 保存登录状态
来源:互联网 发布:传记电影知乎 编辑:程序博客网 时间:2024/05/15 23:46
用C#如何创建、读取cookie:
一、创建cookie,并写入数据:
//创建、写入Cookie
HttpCookie cookie = Request.Cookies["MWS_User"];
if (cookie == null)
{
cookie = new HttpCookie("MWS_User");
}
cookie.Values.Set("UserID", strUserID);
Response.SetCookie(cookie);
二、读取cookie:
HttpCookie cookie = Request.Cookies["MWS_User"];
if (cookie != null && cookie["UserID"].ToString() != "")
{
Response.Write("cookie=" + cookie["UserID"].ToString());
}
例子: 登录页面保存:记住登录状态:保存用户名密码:
1 获取cookie
Page_Load(object sender, EventArgs e)
HttpCookie admincookie =Request.Cookies["userAdmin"];
if (admincookie!=null && admincookie["username"].ToString() != "")
{
this.username.Value = admincookie["username"].ToString();
this.userpass.Attributes.Add("value", admincookie["userpass"].ToString());
}
}
//登录
2 创建cookie
protected void login_submit_btn_Click(object sender, EventArgs e)
{
Maticsoft.BLL.users adminbll = new Maticsoft.BLL.users();
string adminname = username.Value.Trim();
string adminpass = userpass.Text.Trim();
if (Session["Code"] != null && TxtYanZheng.Text.Trim() == Session["Code"].ToString())
{
string whereStr = "name = '" + adminname + "' and password = '" + adminpass + "'";
Maticsoft.Model.users admin = adminbll.GetList(whereStr).Tables[0].Rows.Count != 0 ? adminbll.GetModel(int.Parse(adminbll.GetList(whereStr).Tables[0].Rows[0][0].ToString())) : null;
if (admin != null)
{
if (admin.sign.Trim().Equals("admin"))
{
if (this.CheckBox1.Checked)
{
HttpCookie admincookie = Request.Cookies["userAdmin"];
if (admincookie == null)
{
admincookie = new HttpCookie("userAdmin");
}
admincookie.Values.Set("username", adminname);
admincookie.Values.Set("userpass", adminpass);
admincookie.Expires = System.DateTime.Now.AddMinutes(20);
Response.SetCookie(admincookie);
}
if (new Maticsoft.BLL.users().Update(admin))
{
Session["admin"] = admin;
Response.Redirect("~/Admin/main.aspx");
}
}
}
}
}
移除cookie的 某个值
usersign.Values.Remove(“name”);
- cookie 保存登录状态
- Servlet--保存cookie实现保存用户登录状态
- Android 访问WebService登录通过Cookie保存登录状态
- Android WebView请求,保存cookie,判断登录状态
- 使用cookie实现登录状态的保存及判定
- ShardPreferences 保存登录状态
- 使用Cookie保存菜单状态
- 使用Cookie保存菜单状态
- 用Cookie 保存登录信息
- 通过cookie保存登录密码
- Android WebView保存Cookie登录
- 用Cookie保存登录信息
- Android WebView保存Cookie登录
- OkHttp3登录并保存cookie
- 使用cookie保存用户的登录状态——初步实现
- 使用cookie保存用户的登录状态——初步实现
- 安全cookie登录状态设计方案
- 保存登录名和登录密码cookie
- c# 文件下载
- Java RMI与RPC,JMS的比较
- Android 4.0 ICS SystemUI浅析——StatusBar结构分析
- TI C6000 系列 DSP 之MAP文件
- django前台开发框架
- cookie 保存登录状态
- Android 4.0 ICS SystemUI浅析——StatusBar加载流程分析
- TI C6000 系列 DSP 之系统初始化和系统堆栈
- GDB调试精粹及使用实例
- Touch事件 理 摘
- Source Taste: Activiti: StartProcessInstanceClickListener
- mysql thread入门分析
- 算法设计技巧
- php 发送带附件邮件