一,写一个类来实现

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. Mysql--选择适合的引擎,提高操作速度

    在MySQL 5.1中,MySQL AB引入了新的插件式存储引擎体系结构,允许将存储引擎加载到正在运新的MySQL服务器中   一.数据引擎简介 在MySQL 5.1中,MySQL AB引入了新的插件 ...

  2. hive UDAF开发入门和运行过程详解(转)

    介绍 hive的用户自定义聚合函数(UDAF)是一个很好的功能,集成了先进的数据处理.hive有两种UDAF:简单和通用.顾名思义,简单的UDAF,写的相当简单的,但因为使用Java反射导致性能损失, ...

  3. [转]Creating an iPhone Daemon

    ref: http://chrisalvares.com/blog/7/creating-an-iphone-daemon-part-1/ http://chrisalvares.com/blog/3 ...

  4. TOGAF架构内容框架之架构交付物

    TOGAF架构内容框架之架构交付物 3. 架构交付物(Architecture Deliverables) 架构交付物是在整个架构开发方法循环过程中所产生或被使用的契约性且正规化的企业架构内容,因而其 ...

  5. C# .Net 使用zxing.dll生成二维码,条形码

    public static string GetBarcode(string format, string value, int? width, int? height)        {       ...

  6. Python之路 1Day

    1.python基础入门   作者:黄旭 提示: 语法基于python3.5版本(会提示2.7版本和3.5版本的区别) Python命令行将以>>>开始,比如 >>> ...

  7. VS 2012 单元测试简单配置

    写算法时大多依靠简单输入验证,经常遇到小范围输入正常,大范围或特殊情况输入结果错误的问题.所以写程序之前最好先想好几个特殊用例来测试,以验证算法是否正确.单元测试就是个不错的工具,而VS 2012提供 ...

  8. Fedora 19的U盘安装 以及简单配置

    一.Fedora19的U盘安装 2013年7月3日,Fedora 19正式版发布.Fedora 19除了GNOME桌面版之外,还提供了KDE定制版.LXDE定制版等,有兴趣的网友可以在其官网上下载试用 ...

  9. android引导页的实现 及跳转到主页面

    第一个activity package com.qualitypicture.activity; import java.util.ArrayList; import java.util.List; ...

  10. 美团点评2017校招研发offer面经

    2017届的校招早早就结束了,抽出时间做个记录. 职位:后台开发工程师 岗位职责: 如果你热爱编程,这里给你平台用代码改变世界: 如果你乐于挑战,这里有用户和商家五花八门的需求和苛刻的系统运行环境在等 ...