总结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 ...
随机推荐
- python list 中找连续的数字(由网友处学习)
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #学习这个要求的:http://wsky.org/archives/ ...
- iOS更新之DFU模式和恢复模式
DFU模式和恢复模式的区别:DFU模式是在iPhone固件引导启动之前进行恢复的模式.所以用DFU模式刷机一般比较干净,不会有任何垃圾文件.想当于电脑重新格式化之后再安装系统. DFU模式进入方法:1 ...
- Android——编译odex保护
编译过android源代码的可能试验过改动编译类型.android的初始化编译配置可參考Android--编译系统初始化设置 一.TARGET_BUILD_VARIANT=user 当选择的编译类型为 ...
- OpenGL——点的绘制(使用OpenGL来绘制可旋转坐标系的螺旋线)
package com.example.opengl1; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio. ...
- QtXlsxWriter
Code Issues26 Pull requests2 Pulse Graphs HTTPS clone URL You can clone with HTTPS orSubversion. C ...
- Day_8.《无懈可击的web设计》-巧妙地浮动效果
> 本章内容略显陈旧,主要描述如何用浮动替代表格布局,并没有什么出彩的地方.不过其间提到了清楚浮动的几种方法,那么今天就总结一下如何清楚浮动吧. #### 为什么要清除浮动?虽说是清除浮动,其实 ...
- html5 拖放---(二)转
draggable是一个枚举属性,用于指定一个标签是否可以被拖拽.有以下四种取值: true 表示此元素可拖拽 false 表示此元素不可拖拽 auto 除img和带href的标签a标签表示可拖拽外, ...
- Android SQLite的使用2(非原创)
1.数据库的增.删.改.查:execSQL方法 public void insertAction() {//添加信息 db.execSQL("insert into Emp(name,sal ...
- IIS Hang Troubleshoot
Your website maybe stop working and response very lowly. How to find out the reason? Below are the g ...
- OpenGL ES 2.0 shader开发
1.创建一个shader容器 GLES20.glCreateShader(shaderType); 函数原型为: int glCreateShader (int type) 方法参数: GLES20. ...