总结FormsAuthentication的使用
一、先看一下使用FormsAuthentication做登录认证的用法
用法一:
FormsAuthentication.SetAuthCookie(username, isPersistent);
用法二:
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
username,
DateTime.Now,
DateTime.Now.AddMinutes(720),
isPersistent,
userData,
FormsAuthentication.FormsCookiePath); // 加密票证
string encTicket = FormsAuthentication.Encrypt(ticket); // 创建cookie
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
HttpContext.Current.Response.Cookies.Add(cookie);
二、几个问题
1.用法一和用法二区别?
2.用法一中的认证过期时间是如何设置的?用法二中又是如何设置的?
3.用法二中票证中设置过期时间和Web.config中设置的过期时间哪个优先?
4.用法二中userData是否可以为null?
5.用法中的isPersistent是什么?
6.什么是持久化cookie?什么是会话性cookie?
7.用法二中的isPersistent为何没起作用?
三、解答
1.用法一无法使用userData数据。
2.用法一中的过期时间为Web.config中配置的时间。
3.用法二中票证中设置的过期时间优先于Web.config中设置的过期时间(即以票证中设置的过期时间为准)。
4.用法二中userData不可为null,否则无法加密票证。详见MSDN文档。
5.isPersistent表示是否要持久化认证cookie。
6.持久化cookie保存在物理介质中。(win7中的位置为C:\Users\用户名\AppData\Roaming\Microsoft\Windows\Cookies,注意Appdata是个隐藏的文件夹)
会话性cookie保存于内存中。关闭浏览器则会话性cookie会过期消失;持久化cookie则不会,直至过期时间已到或确认注销。
注意:如果要使用会话性cookie,就不能为cookie对象设置过期时间,一旦设置了过期时间就为持久化cookie。
7.其实用法二是大家常见的写法,但这种写法并没有使isPersistent。参考以下写法:
public static void SetFormsAuthentication(string username, bool isPersistent, string userData)
{
userData = string.IsNullOrEmpty(userData) ? string.Empty : userData; //创建票证
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
username,
DateTime.Now,
DateTime.Now.AddMinutes(720),
isPersistent,
userData,
FormsAuthentication.FormsCookiePath); // 加密票证
string encTicket = FormsAuthentication.Encrypt(ticket); // 创建cookie
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket)
{
HttpOnly = true,
Path = FormsAuthentication.FormsCookiePath,
Secure = false
};
if (ticket.IsPersistent)
{
cookie.Expires = ticket.Expiration;
}
HttpContext.Current.Response.Cookies.Add(cookie);
}
总结FormsAuthentication的使用的更多相关文章
- 基于FormsAuthentication的用户、角色身份认证
一般情况下,在我们做访问权限管理的时候,会把用户的正确登录后的基本信息保存在Session中,以后用户每次请求页面或接口数据的时候,拿到 Session中存储的用户基本信息,查看比较他有没有登录和能否 ...
- Nancy FormsAuthentication使用
1.新建UserDatabase类,实现IUserMapper接口 using System; using System.Collections.Generic; using System.Linq; ...
- FormsAuthentication身份认证源代码
使用FormsAuthentication类可以实现身份认证功能,这里提供一个asp.net项目的源代码,项目名称KWS.项目实现了登录.退出和判断身份的功能. 关于项目 点击这里下载源代码 http ...
- FormsAuthentication详解
配置安全鉴别 鉴别是指鉴定来访用户是否合法的过程.ASP.NET Framework支持三种鉴别类型: Windows鉴别: NET Passport鉴别: Forms鉴别. 对于某一特定的应用程序, ...
- asp.net 登陆验证 Form表单验证的3种方式 FormsAuthentication.SetAuthCookie;FormsAuthentication.RedirectFromLoginPage;FormsAuthenticationTicket
我们在登陆成功后,使用下面的3种方法,都是同一个目的:创建身份验证票并将其附加到 Cookie, 当我们用Forms认证方式的时候,可以使用HttpContext.Current.User.Ident ...
- 自己实现FormsAuthentication.SetAuthCookie方法,怎样在ASP.NET服务端代码中删除客户端Cookie
如何手动设置AuthCookie ASP.NET中实现可以自己实现FormsAuthentication.SetAuthCookie方法,控制更为灵活 /// <summary> /// ...
- FormsAuthentication.HashPasswordForStoringInConfigFile 方法 之研究
摘自:http://time-is-life.cnblogs.com/articles/322523.html 给定标识哈希类型的密码和字符串,该例程产生一个适合存储在配置文件中的哈希密码. [C#] ...
- 利用FormsAuthentication.RedirectFromLoginPage进行身份验证
web.config中: <authentication>节 格式: <authentication mode="Forms"> //I.Window ...
- [ASP.NET]更简单的方法:FormsAuthentication登录ReturnUrl使用绝对路径
转自:http://www.cnblogs.com/dudu/p/formsauthentication-returnurl-absoluteuri.html [ASP.NET]更简单的方法:Form ...
- FormsAuthentication与Session超时时间不一的解决方法
因为FormsAuthentication 和 Session 的cookies不一样,造成了FormsAuthentication 还能进入,而 session已经超时的问题. 最好的办法就是当让F ...
随机推荐
- Ubuntu Eclipse的Tomcat小问题:不能输入server name,不能启动tomcat
Ubuntu的Eclipse上安装Tomcat环境,这是让人烦啊,万幸还是终于解决了. Eclipse上Tomcat的搭建: 1.点击Eclipse上的菜单:Windows - Preference, ...
- Swiper滑动Html5手机浏览器自适应
手机网页能通过window.screen.height, width获取屏幕分辨率,于是能够通过分辨率比率来计算高度. window.onload=function(){ var swiper = d ...
- HDU 1827 Summer Holiday(Tarjan缩点)
Problem Description To see a World in a Grain of Sand And a Heaven in a Wild Flower, Hold Infinity ...
- 2014 (多校)1011 ZCC Loves Codefires
自从做了多校,整个人都不好了,老是被高中生就算了,题老是都不懂=-=原谅我是个菜鸟,原谅我智力不行.唯一的水题. Problem Description Though ZCC has many Fan ...
- go中string和slice no-copy转换
在go里面,string和slice的互换是需要进行内存拷贝的,虽然在底层,它们都只是用 pointer + len来表示的一段内存. 通常,我们不会在意string和slice的转换带来的内存拷贝性 ...
- jxl读写excel的方法
jxl 只有excel基本的操作,代码操作比较方便,一般使用jxl就够了,对图片支持较好 poi功能比jxl强大但是比较吃内存,支持计算公式 具体参考链接 http://www.cnblogs. ...
- el表达式判断是否相等
<c:if test="${order.baofang eq 0 }"> 无包间 </c:if> <c:if test="${order.b ...
- SQLLoader5(从多个数据文件导入到同一张表)
从多个数据文件导入到同一张表很简单,只需要在INFILE参数指定多个数据文件的路径即可.数据文件1:test1.txt1111 ALLE SALESMAN2222 WARD SALESMAN数据文件2 ...
- 我的第一个MVC项目
这是本人第一个完全按照MVC开发模式完成的项目,非常简陋,仅仅完成的查询,保存,删除基本功能,而且存在编码问题没有解决,以后会慢慢增加功能.慢慢改进 package com.sdjt.dao; imp ...
- 调用[[UIDevice currentDevice] userInterfaceIdiom]==UIUserInterfaceIdiomPad判断设备
将模拟器改为Ipad时,调用[[UIDevice currentDevice] userInterfaceIdiom]==UIUserInterfaceIdiomPad判断设备是否为Ipad,但程序并 ...