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

推荐订阅源

罗磊的独立博客
SecWiki News
SecWiki News
酷 壳 – CoolShell
酷 壳 – CoolShell
爱范儿
爱范儿
量子位
M
MIT News - Artificial intelligence
GbyAI
GbyAI
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
TaoSecurity Blog
TaoSecurity Blog
博客园 - 【当耐特】
H
Heimdal Security Blog
腾讯CDC
The Last Watchdog
The Last Watchdog
Security Archives - TechRepublic
Security Archives - TechRepublic
Hacker News: Ask HN
Hacker News: Ask HN
S
Schneier on Security
Microsoft Security Blog
Microsoft Security Blog
WordPress大学
WordPress大学
博客园 - 司徒正美
Recent Commits to openclaw:main
Recent Commits to openclaw:main
C
Cybersecurity and Infrastructure Security Agency CISA
S
SegmentFault 最新的问题
大猫的无限游戏
大猫的无限游戏
Application and Cybersecurity Blog
Application and Cybersecurity Blog
F
Full Disclosure
有赞技术团队
有赞技术团队
T
Tailwind CSS Blog
Engineering at Meta
Engineering at Meta
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Threatpost
月光博客
月光博客
A
Arctic Wolf
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
雷峰网
雷峰网
T
Troy Hunt's Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The Cloudflare Blog
D
DataBreaches.Net
O
OpenAI News
L
LINUX DO - 最新话题
宝玉的分享
宝玉的分享
小众软件
小众软件
V
Vulnerabilities – Threatpost
A
About on SuperTechFans
人人都是产品经理
人人都是产品经理
T
The Exploit Database - CXSecurity.com
Martin Fowler
Martin Fowler
美团技术团队
P
Privacy International News Feed

博客园 - 疾风

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