总结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 ...
随机推荐
- [转载]用可变参数宏(variadic macros)传递可变参数表
注意:_VA_ARGS__ 从VS2005才开始支持 在 GNU C 中,宏可以接受可变数目的参数,就象函数一样,例如: #define pr_debug(fmt,arg...) printk(KER ...
- errno的基本用法
error是一个包含在 perror()和strerrot()函数可以把errno的值转化为有意义的字符输出. #include <stdio.h> #include <stdlib ...
- 移动支付之智能IC卡与Android手机进行NFC通信
本文来自http://blog.csdn.net/hellogv/ .引用必须注明出处. 眼下常见的智能IC卡执行着JavaCard虚拟机.智能IC卡上能够执行由精简后的Java语言编写 ...
- 谈谈UIView的几个layout方法-layoutSubviews、layoutIfNeeded、setNeedsLayout...
最近在学习swift做动画,用到constraint的动画,用到layoutIfNeeded就去研究了下UIView的这几个布局的方法. 下面是做得一个动画,下载地址:https://github.c ...
- ThinkPHP实现跨模块调用操作方法概述
ThinkPHP实现跨模块调用操作方法概述 投稿:shichen2014 字体:[增加 减小] 类型:转载 使用 $this 可以调用当前模块内的方法,但是很多情况下经常会在当前模块中调用其他模块 ...
- COM口,串行通讯端口,RS-232接口 基础知识
COM口即串行通讯端口. COM口的接口标准规范和总线标准规范是RS-232,有时候也叫做RS-232口.电脑上的com口多为9针,最大速率115200bps.通常用于连接鼠标(串口)及通讯设备(如连 ...
- FLASH图片上传功能—从百度编辑器UEditor里面提取出来
为了记录工作中碰到的各种问题,以及学习资料整理,今天开始,将以往的文章进行了一个整理,以后也开始认真的记录学习过程中的各种问题 在HTML里面的文件上传功能一直是个问题,为了实现上传文件大小限制,怎样 ...
- ios10 适配问题总结
看各个大神整理而成 1.检查版本问题 不可以像下面这样用 #define isiOS10 ([[[[UIDevice currentDevice] systemVersion] substringTo ...
- [翻译] C++ STL容器参考手册(第一章 <array>)
返回总册 本章节原文:http://www.cplusplus.com/reference/array/array/ 1. std::array (C++11支持) template < cla ...
- Spring连接MySQL、Oracle和SQL Server的数据库运动连接属性
在配置文件applicationContext.xml设置如下:<?xml version="1.0" encoding="UTF-8"?>< ...