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 ...
随机推荐
- ASP.NET WebAPI HTTPS
参照文档 http://southworks.com/blog/2014/06/16/enabling-ssl-client-certificates-in-asp-net-web-api/ 第一 ...
- Page_Load接收随机参数放到字典类中
Page_Load接收随机参数放到字典类中,可以用作签名.普通的接收url的参数可以用作下面这种模式: int appid =Convert.ToInt32(param["appid&qu ...
- DNS:域名系统
DNS:域名系统 1.DNS DNS 是计算机域名系统(Domain Name System 或Domain Name Service) 的缩写,它是由解析器以及域名服务器组成的.域名服务器 ...
- SSH的整合
SSH的整合 struts2和hibernate的配置我这里就不多说了,先把两个有关的东西说下.一个是所有的包.struts2+hibernate3+spring2.5我包准备放上去给大家下载. ht ...
- HttpMessageHandler管道[上篇]
HttpMessageHandler管道[上篇] 整个ASP.NET Web API服务端框架采用管道式设计,这个用于“处理请求.响应回复”的管道本质上就是一组HttpMessageHandler的有 ...
- 改善C#公共程序类库质量的10种方法和工具
最近重构一套代码,运用以下几种方法,供参考. 1 公共方法尽可能的使用缓存 public static List<string> GetRegisteredCompany() { Str ...
- C#基础之方法和参数
C#基础之方法和参数 接上一篇<C#基础之类型和成员基础以及常量.字段.属性> 实例方法.静态方法 C#中的方法分为两类,一种是属于对象(类型的实例)的,称之为实例方法,另一种是属于类型的 ...
- The Event System
The Event System 在Qt中,事件是继承了虚拟类QEvent的对象,它代表了程序所发生的事情或者程序需要知道的一个外部活动的结果.事件可以被任意 QObject子类的实例接收和处理,是与 ...
- Swift3.0服务端开发(四) MySQL数据库的连接与操作
本篇博客我们来聊聊MySQL数据库的连接与操作.如果你本地没有MySQL数据库的话,需要你先安装MySQL数据库.在Mac OS中使用brew包管理器进行MySQL的安装是及其方便的.安装MySQL的 ...
- mac 显示隐藏文件方法
终端执行命令: 显示:#defaults write com.apple.finder AppleShowAllFiles -bool true隐藏:#defaults write com.apple ...