FormsCookieName保存登录用户名的使用
一,写一个类来实现
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security; /// <summary>
///FormsCookieNameTest 的摘要说明
/// </summary>
public class FormsCookieNameTest
{
/// <summary>
/// FormsAuthenticationTicket
/// </summary>
/// <param name="uname"></param>
public bool Login(string name)
{
if (!string.IsNullOrEmpty(name))
{
//FormsAuthentication.SetAuthCookie(uname,true);
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket
(,
name,
DateTime.Now,
DateTime.Now.AddMinutes(),
true,
"",
"/"
);
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket));
cookie.HttpOnly = true;
HttpContext.Current.Response.Cookies.Add(cookie);
return true;
}
return false;
}
}
二,C#中的FormsAuthenticationTicket解析
//
// 摘要:
// 使用 cookie 名、版本、目录路径、发布日期、过期日期、持久性以及用户定义的数据初始化 System.Web.Security.FormsAuthenticationTicket
// 类的新实例。
//
// 参数:
// version:
// 票证的版本号。
//
// name:
// 与票证关联的用户名。
//
// issueDate:
// 票证发出时的本地日期和时间。
//
// expiration:
// 票证过期时的本地日期和时间。
//
// isPersistent:
// 如果票证将存储在持久性 Cookie 中(跨浏览器会话保存),则为 true;否则为 false。如果该票证存储在 URL 中,将忽略此值。
//
// userData:
// 存储在票证中的用户特定的数据。
//
// cookiePath:
// 票证存储在 Cookie 中时的路径。
[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public FormsAuthenticationTicket(int version, string name, DateTime issueDate, DateTime expiration, bool isPersistent, string userData, string cookiePusing System;
三,一个登陆保存cookie和删除cookie
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security; public partial class FormsCookieName : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
FormsCookieNameTest t= new FormsCookieNameTest();
if (t.Login(TextBox1.Text))
{
Response.Redirect("~/FormsCookieName1.aspx");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security; public partial class FormsCookieName1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ } protected void Button1_Click(object sender, EventArgs e)
{
//当在页面FormsCookieName2删除cookie,则这里则为空,就是退出登录时
var cookie = Request.Cookies[FormsAuthentication.FormsCookieName];
if (cookie != null)
{
var ticket = FormsAuthentication.Decrypt(cookie.Value);
string role = ticket.UserData;
TextBox1.Text = ticket.Name;
}
else {
TextBox3.Text = "cookie删除成功";
}
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Redirect("~/FormsCookieName2.aspx");
}
}
protected void Page_Load(object sender, EventArgs e)
{
//删除
Response.Cookies[FormsAuthentication.FormsCookieName].Expires = DateTime.Now.AddMinutes(-);
CookieExtensions.DeleteCookie(FormsAuthentication.FormsCookieName);
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("~/FormsCookieName1.aspx");
}
这两个页面要注意的是:获取ticket.Name写在和你生成cookie的同一个页面的时候,会出现你获取的ticket.Name的值为上一个cookie的ticket.Name的值。
FormsCookieName保存登录用户名的使用的更多相关文章
- C#保存登录用户名供其他页面调用
一.保存登录用户名供其他页面调用 步骤: (1)项目自带的Program.cs,类方法里定义登录的用户名为全局变量loginid,这样整个项目都可以调用它 static class Program { ...
- cookie保存登录的用户名和密码
用cookie保存登录的用户名和密码,当用户访问网站的时候,获取cookie的用户名和密码,通过用 用cookie保存登录的用户名和密码,当用户访问网站的时候,获取cookie的用户名和密码,通过用户 ...
- 保存登录信息的Cookie加密技术
所有需要账户登录的website 基本都会想到这样一个问题, 如何保持用户在一定时间内登录有效. 最近本人就在项目中遇到这样的需求,某些页面只能Admin账户登录后访问, 当登录Admin账户后如何才 ...
- 在mac中自动保存git用户名与密码如此简单
之前为了实现在Windows中自动保存git用户名与密码,写过一篇博客终于解决“Git Windows客户端保存用户名与密码”的问题,需要进行一堆配置. 而在Mac OS X中这个操作竟然如此简单.只 ...
- 创建、显示和删除保存的用户名和密码(cmdkey)
创建,显示和删除保存的用户名和密码: cmdkey.exe /add:targetname /user:username /pass:password
- 修改 VSS 默认登录用户名三种方法
修改 VSS 默认登录用户名三种方法标签: VSS VSS2005c#2014-11-27 10:27 1561人阅读 评论(0) 收藏 举报 分类: VSS软件开发总会有 BUG 和更新的需求,之前 ...
- JMeter 怎么保存登录状态
在Recording Controller中添加一个HTTP Cookie Manager Recording Controller右键-->add-->config element--& ...
- git删除掉已经保存的用户名密码
以保存的用户名密码删除,先找到变量存在的位置: git config -l To help track down the setting, I'd try to use: git config --l ...
- XManager&XShell如何保存登录用户和登录密码
Xshell配置ssh免密码登录 - qingfeng2556的博客 - CSDN博客https://blog.csdn.net/wuhenzhangxing/article/details/7948 ...
随机推荐
- 企业架构研究总结(41)——企业架构与建模之ArchiMate的由来和详述(上)
终于完成了关于企业架构框架理论的总结,谢谢各位看官的支持,能挺过之前过于理论化的叙述而坚持到现在着实不易,笔者也自愧没有实践经验可以分享,希望日后有兴趣的看官能够不吝赐教.在本系列后面的也是最后一个大 ...
- js闭包(转)
一.变量的作用域 要理解闭包,首先必须理解Javascript特殊的变量作用域. 变量的作用域无非就是两种:全局变量和局部变量. Javascript语言的特殊之处,就在于函数内部可以直接读取全局变量 ...
- SQLSERVER之高灵活的业务单据流水号生成
SQLSERVER之高灵活的业务单据流水号生成 最近的工作中要用到流水号,而且业务单据流水号生成的规则分好几种,并非以前那种千篇一律的前缀+日期+流水号的简单形式,经过对业务的分析,以及参考网上程序员 ...
- uploadify的使用
uploadify的使用 课程设计需要实现上传文件模块,本来ASP.NET是有内置的控件,但是ASP.NET MVC没有,所以就有两种方法:自定义和采用第三方插件.由于时间的关系,故采用第三方插件:u ...
- BST&AVL&红黑树简单介绍
(BST&AVL&红黑树简单介绍) 前言: 节主要是给出BST,AVL和红黑树的C++代码,方便自己以后的查阅,其代码依旧是data structures and algorithm ...
- 微软必应借PK谷歌突围中国搜索市场
Bing“必应”是微软2009年推出的搜索品牌(http://www.bing.com),它取代的是同门师兄Live Search.进入2013年,在国内及好莱坞的多部大片里面,我都看到了Bing搜索 ...
- Hackers’ Crackdown-----UVA11825-----DP+状态压缩
题目地址:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- Javascript 闭包与变量
1.闭包与变量 JavaScript中的作用域链的机制引出了一个副作用,即闭包只能取得包含函数中任何变量的最后一个值.闭包所保存的是整个变量对象,而不是某个特殊的值. 1 2 3 4 5 6 7 8 ...
- Spring MVC---基于注解的控制器
基于注解的控制器 SpringMVC是一个基于DispatcherServlet的MVC框架,每个请求最先访问的是Dispatcher ...
- Python小问题汇总
现在的时间适合写点最近的小总结,这中间涉及到python/git等问题,我就从python先说起吧. 一.Python 1. Python的异常处理 因为想到自己不断尝试写小程序的话会用到抛出异常信息 ...