惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Stack Overflow Blog
Stack Overflow Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
大猫的无限游戏
大猫的无限游戏
爱范儿
爱范儿
WordPress大学
WordPress大学
B
Blog RSS Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
人人都是产品经理
人人都是产品经理
J
Java Code Geeks
酷 壳 – CoolShell
酷 壳 – CoolShell
小众软件
小众软件
MyScale Blog
MyScale Blog
GbyAI
GbyAI
Martin Fowler
Martin Fowler
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 聂微东
The Cloudflare Blog
L
Lohrmann on Cybersecurity
Apple Machine Learning Research
Apple Machine Learning Research
I
InfoQ
Google DeepMind News
Google DeepMind News
S
Securelist
Application and Cybersecurity Blog
Application and Cybersecurity Blog
博客园 - 【当耐特】
Latest news
Latest news
T
Threatpost
量子位
Y
Y Combinator Blog
T
Troy Hunt's Blog
Know Your Adversary
Know Your Adversary
MongoDB | Blog
MongoDB | Blog
罗磊的独立博客
博客园_首页
AWS News Blog
AWS News Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
宝玉的分享
宝玉的分享
Project Zero
Project Zero
V
Visual Studio Blog
F
Fortinet All Blogs
S
Security Affairs
The Register - Security
The Register - Security
G
Google Developers Blog
T
Tenable Blog
L
LINUX DO - 最新话题
The GitHub Blog
The GitHub Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
博客园 - 三生石上(FineUI控件)
T
The Exploit Database - CXSecurity.com
博客园 - Franky

博客园 - 疾风

Internet Explorer允许的URL最大长度为2083个字符(Maximum URL length is 2,083 characters in Internet Explorer) Eval和Bind 古筝和古琴有什么区别? 认识CSS中absolute与relative BAT详细手册 iframe的使用 领域逻辑的实现分类 面向对象问答 用户点击Log In按钮后发生了什么? 微软的在线RSS阅读器 2003年至2006年项目总结 经常会有一些Gmail的邀请函 Visual Studio 2005 RTM的奇怪问题 在.Net代码中验证XML文档 2006年的一些目标 创建强类型的程序集 在.net中使用强类型来读取配置信息 自己做了一个单元测试的小工具---EasyTest.net How to gets the path for the executable file?
用户点击Log In按钮后发生了什么?(续篇1)
疾风 · 2006-06-09 · via 博客园 - 疾风

    上篇我们说到在Login控件的AttemptLogin方法中,最终使用了FormAuthentication的SetAuthCookie(string userName, bool createPersistentCookie)
来设置Cookie,那具体是怎么工作的呢?让我们看看它的代码:

1 public static void SetAuthCookie(string userName, bool createPersistentCookie)
2 {
3       FormsAuthentication.Initialize();
4       FormsAuthentication.SetAuthCookie(userName, createPersistentCookie, FormsAuthentication.FormsCookiePath);
5 }

    嗯,首先调用Initialize()方法初始化一把,察看Initialize()的代码得知它的主要作用是设置一下FormAuthentication的基本参数,包括_FormsName、_RequireSSL、_FormsCookiePath、_TimeOut、_LoginUrl、DefaultUrl 等,最后把_Initialized内部状态设置成true(其中使用了线程同步锁)。

    然后使用由Initialize()方法中设置好的FormsAuthentication.FormsCookiePath属性来调用FormsAuthentication.SetAuthCookie(userName, createPersistentCookie, FormsAuthentication.FormsCookiePath)方法,让我们先看看MSDN怎么描述这个方法的功能:

    Creates an authentication ticket for the supplied user name and adds it to the cookies collection of the response, using the supplied cookie path or the URL.

    再来看看这个方法的代码:

 1 public static void SetAuthCookie(string userName, bool createPersistentCookie, string strCookiePath)
 2 {
 3       
 4       HttpContext context1 = HttpContext.Current;
 5       if (!context1.Request.IsSecureConnection && FormsAuthentication.RequireSSL)
 6       {
 7             throw new HttpException(SR.GetString("Connection_not_secure_creating_secure_cookie"));
 8       }
 9       bool flag1 = CookielessHelperClass.UseCookieless(context1, false, FormsAuthentication.CookieMode);
10       HttpCookie cookie1 = FormsAuthentication.GetAuthCookie(userName, createPersistentCookie, flag1 ? "/" : strCookiePath, !flag1);
11       if (!flag1)
12       {
13             HttpContext.Current.Response.Cookies.Add(cookie1);
14             context1.CookielessHelper.SetCookieValue('F'null);
15       }
16       else
17       {
18             context1.CookielessHelper.SetCookieValue('F', cookie1.Value);
19       }
20 }
21 

    其中CookielessHelperClass.UseCookieless(...)方法的功能是判断当前请求上下文是否接受本地Cookie(不接受的话返回true),如果请求上下文接受本地Cookie则直接把Cookie加到请求上下文中,如果请求上下文不接受本地Cookie则把Cookie的值放入名为“F”的Cookie值中,以待后用。
    读到这里,我们已经清楚地知道用户点击Log In按钮后发生了什么事、Asp.net 2.0是如何记录登录信息的了,但是如果想更清楚地知道这个Cookie是如何生成的,还得去读读 FormsAuthentication.GetAuthCookie (String, Boolean, String) 方法。