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

推荐订阅源

S
SegmentFault 最新的问题
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
博客园 - 【当耐特】
月光博客
月光博客
Vercel News
Vercel News
D
Docker
I
InfoQ
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 叶小钗
MongoDB | Blog
MongoDB | Blog
GbyAI
GbyAI
有赞技术团队
有赞技术团队
雷峰网
雷峰网
博客园 - 聂微东
小众软件
小众软件
Y
Y Combinator Blog
腾讯CDC
L
LangChain Blog
The GitHub Blog
The GitHub Blog
宝玉的分享
宝玉的分享
Stack Overflow Blog
Stack Overflow Blog
大猫的无限游戏
大猫的无限游戏
T
The Blog of Author Tim Ferriss

博客园 - 疾风

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) 方法。