一,写一个类来实现

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保存登录用户名的使用的更多相关文章

  1. C#保存登录用户名供其他页面调用

    一.保存登录用户名供其他页面调用 步骤: (1)项目自带的Program.cs,类方法里定义登录的用户名为全局变量loginid,这样整个项目都可以调用它 static class Program { ...

  2. cookie保存登录的用户名和密码

    用cookie保存登录的用户名和密码,当用户访问网站的时候,获取cookie的用户名和密码,通过用 用cookie保存登录的用户名和密码,当用户访问网站的时候,获取cookie的用户名和密码,通过用户 ...

  3. 保存登录信息的Cookie加密技术

    所有需要账户登录的website 基本都会想到这样一个问题, 如何保持用户在一定时间内登录有效. 最近本人就在项目中遇到这样的需求,某些页面只能Admin账户登录后访问, 当登录Admin账户后如何才 ...

  4. 在mac中自动保存git用户名与密码如此简单

    之前为了实现在Windows中自动保存git用户名与密码,写过一篇博客终于解决“Git Windows客户端保存用户名与密码”的问题,需要进行一堆配置. 而在Mac OS X中这个操作竟然如此简单.只 ...

  5. 创建、显示和删除保存的用户名和密码(cmdkey)

    创建,显示和删除保存的用户名和密码: cmdkey.exe /add:targetname /user:username /pass:password

  6. 修改 VSS 默认登录用户名三种方法

    修改 VSS 默认登录用户名三种方法标签: VSS VSS2005c#2014-11-27 10:27 1561人阅读 评论(0) 收藏 举报 分类: VSS软件开发总会有 BUG 和更新的需求,之前 ...

  7. JMeter 怎么保存登录状态

    在Recording Controller中添加一个HTTP Cookie Manager Recording Controller右键-->add-->config element--& ...

  8. git删除掉已经保存的用户名密码

    以保存的用户名密码删除,先找到变量存在的位置: git config -l To help track down the setting, I'd try to use: git config --l ...

  9. XManager&XShell如何保存登录用户和登录密码

    Xshell配置ssh免密码登录 - qingfeng2556的博客 - CSDN博客https://blog.csdn.net/wuhenzhangxing/article/details/7948 ...

随机推荐

  1. 设计模式之观察者模式(Observable与Observer)

    设计模式之观察者模式(Observable与Observer) 好久没有写博客啦,之前看完了<设计模式之禅>也没有总结一下,现在回忆一下设计模式之观察者模式. 1.什么是观察者模式 简单情 ...

  2. discuz 门户功能增加自定义keywords字段

    discuz的门户的“发布文章”功能中,没有自动添加keywords字段,结果在文章页面中的meta的keywords中只显示标题,这样对于seo及其不利,今天整理了添加keywords字段方法. 一 ...

  3. IOS基础开发二(iphone计算器)

    今天做了个iphone的小例子计算器:才用mvc设计模式 项目目录: 代码如下: CalculatorViewController:(MVC的控制器) // CalculatorViewControl ...

  4. 初探中间件(middleware)

    初探中间件(middleware) 因为考虑到文章的长度, 所以 BaseHandler 的展开被推迟了. 在 BaseHandler 中隐藏着中间件的信息, 较常见的 SessionMiddlewa ...

  5. Javascript 封装问题

    Javascript 封装问题 为什么会用这样一个题目呢,这是要说封装的什么问题,本文并不讲高深的封装理论,只是解决一个小问题. 问题来源 今天在百度知道上闲逛,遇到一个网友的问题,问题如下,问题的地 ...

  6. .net postsharp编译时生成的代码?

    使用PostSharp进行AOP框架设计:一个简单的原型   AOP已经不是一个什么新名词了,在博客园使用关键字搜索可以查出n多条关于AOP的介绍,这里就不再赘述了. 在Bruce Zhang's B ...

  7. Android中系统设置中的清除数据究竟会清除哪些数据

    今天中的一个story突然提到了系统设置中的清理数据,后来开始思考究竟系统的应用的这个清理功能,究竟会清理那些数据. 于是开始研究,以com.mx.browser为例,思路大概为首先为/data/da ...

  8. Android Phone和Pad UA区别

    很多Android开发者或者网站端都可能会困扰关于如何区分Android phone和Android Pad的ua.确实这个问题很困难,我也曾被困扰了一段时间,后来在Stackoverflow中发现了 ...

  9. Web开发必回知识点

    Web前端必须知道 一.常用那几种浏览器测试?有哪些内核(Layout Engine)? 1.浏览器:IE,Chrome,FireFox,Safari,Opera. 2.内核:Trident,Geck ...

  10. 如何在网站中加入markdown

    在vue组件中加入markdown,模板使用的是webpack 我是这样做的: 因为是npm引入的,所以markdown是遵循CommonJS规范的,需要在webpack.base.conf.js里引 ...