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 ...
随机推荐
- IceMx.Mvc
IceMx.Mvc 我的js MVC 框架 开篇 开篇 这篇文章是后补的,前端时间想写一些对于js开发的一些理解,就直接写了,后来发现很唐突,所以今天在这里补一个开篇. 我的js Mvc 框架 基于实 ...
- key-list类型内存数据引擎介绍及使用场景
“互联网数据目前基本使用两种方式来存储,关系数据库或者key value.但是这些互联网业务本身并不属于这两种数据类型,比如用户在社会化平台中的关系,它是一个list,如果要用关系数据库存储就需要转换 ...
- ASP.NET MVC学习笔记-----Filter
ASP.NET MVC学习笔记-----Filter(1) Filter类型 接口 MVC的默认实现 Description Authorization IAuthorizationFilter Au ...
- 【最新】让快捷方式 实现相对路径——非.bat方式实现
快捷方式一般是使用绝对路径的,但并不是不能使用相对路径,系统为普通用户考虑一般默认使用绝对路径,这样的结果是:只要目标文件(不论是文件夹.程序.文档,也包括快捷方式)绝对位置不变,我们创建的快捷方式基 ...
- 安装linux版qq,安装二进制包编译器,安装mysql-5.6.11,删除已安装或安装失败的mysql-5.6.11,简单mysql练习题
上午[root@localhost ~]# ./test3.sh dev1^C[root@localhost ~]# groupadd dev1[root@localhost ~]# vim /etc ...
- vim 多行同时输入,且输入数值递增
很有用的命令. 很给力的说. http://vim.wikia.com/wiki/Making_a_list_of_numbers 我在 html中需要增加新的标签的时候,就有用到过. 原来的html ...
- hdu 1253 胜利大逃亡(BFS)
题目链接:点击链接 三维的BFS,刚开始一直超内存,超无语...... 改了n多次终于AC了 #include <iostream> #include <stdio.h> # ...
- C语言面试题汇总
1. 阅读下面程序并写出输出结果(10分). main() { int a[5]={1,2,3,4,5}; int *ptr=(int *)(&a+1); printf("%d, ...
- [置顶] Oracle学习经验谈
经常遇到朋友问oracle学习难不难,怎么才能成为高手等等,我想结合我的个人经验简单说几点: 1.打好基础,由浅入深 学习Oracle不能急于求成,寄希望于一天成为一个大侠.学习有个过程,应该由浅入深 ...
- C#中数组,ArrayList与List对象的区别
在C#中,当我们想要存储一组对象的时候,就会想到用数组,ArrayList,List这三个对象了.那么这三者到底有什么样的区别呢? 我们先来了解一下数组,因为数组在C#中是最早出现的. 数组 数组有很 ...