asp.net forms
这篇文章之前 回忆起当初最初的时候 做登录。
直接获取控件中的值。 然后和数据库中对比 作比较。
正确就直接 存入session。:)
<fieldset> <legend>普通登录</legend> <form action="<%= Request.RawUrl %>" method="post"> 登录名:<input type="text" name="loginName" style="width: 200px" value="Fish" /> <input type="submit" name="NormalLogin" value="登录" /> </form> </fieldset> <fieldset> <legend>用户状态</legend> <form action="<%= Request.RawUrl %>" method="post"> <% if (Request.IsAuthenticated) { %> 当前用户已登录,登录名:<%= Context.User.Identity.Name %> <br /> <input type="submit" name="Logon" value="退出" /> <% } else { %> <b>当前用户还未登录。</b> <% } %> </form> </fieldset>
protected void Page_PreInit(object sender, EventArgs e) { Login(); Logon(); } public void Logon() { string loginName = Request.Form["Logon"]; if (string.IsNullOrEmpty(loginName)) return; FormsAuthentication.SignOut(); } public void Login() { string loginName = Request.Form["loginName"]; if (string.IsNullOrEmpty(loginName)) return; FormsAuthentication.SetAuthCookie(loginName, true); }
代码 很简单。
如果Logon 传过来 注销登陆
如果LoginName 传过来。 登陆
SetAuthCookie 里面具体干了什么呢?
public static void SetAuthCookie(string userName, bool createPersistentCookie, string strCookiePath) { Initialize(); HttpContext current = HttpContext.Current; if (!current.Request.IsSecureConnection && RequireSSL) { throw new HttpException(SR.GetString("Connection_not_secure_creating_secure_cookie")); } bool flag = CookielessHelperClass.UseCookieless(current, false, CookieMode); HttpCookie cookie = GetAuthCookie(userName, createPersistentCookie, flag ? "/" : strCookiePath, !flag); if (!flag) { HttpContext.Current.Response.Cookies.Add(cookie); current.CookielessHelper.SetCookieValue('F', null); } else { current.CookielessHelper.SetCookieValue('F', cookie.Value); } }
就是检查一下。 然后写入cookie
在浏览器 发现生成了两条cookie
只要你将LoginCookieName 删除 就会发现 你登陆没了..
用户登录的过程大致是这样的:
1. 检查用户提交的登录名和密码是否正确。
2. 根据登录名创建一个FormsAuthenticationTicket对象。
3. 调用FormsAuthentication.Encrypt()加密。
4. 根据加密结果创建登录Cookie,并写入Response。
在登录验证结束后,一般会产生重定向操作, 那么后面的每次请求将带上前面产生的加密Cookie,供服务器来验证每次请求的登录状态。每次请求时的(认证)处理过程如下:
1. FormsAuthenticationModule尝试读取登录Cookie。
2. 从Cookie中解析出FormsAuthenticationTicket对象。过期的对象将被忽略。
3. 根据FormsAuthenticationTicket对象构造FormsIdentity对象并设置HttpContext.User
4. UrlAuthorizationModule执行授权检查。
大概的过程是这样的。
也可以看看这里 微软给出了 很好的列子 猛击这里
private void Login_Click(Object sender, EventArgs e) { // Create a custom FormsAuthenticationTicket containing // application specific data for the user. string username = UserNameTextBox.Text; string password = UserPassTextBox.Text; bool isPersistent = PersistCheckBox.Checked; if (Membership.ValidateUser(username, password)) { string userData = "ApplicationSpecific data for this user."; FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, username, DateTime.Now, DateTime.Now.AddMinutes(30), isPersistent, userData, FormsAuthentication.FormsCookiePath); // Encrypt the ticket. string encTicket = FormsAuthentication.Encrypt(ticket); // Create the cookie. Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket)); // Redirect back to original URL. Response.Redirect(FormsAuthentication.GetRedirectUrl(username, isPersistent)); } else { Msg.Text = "Login failed. Please check your user name and password and try again."; } }
这里就算一个简单的自定义 forms 身份验证
自定义身份验证大概分 以下步骤
1. 在登录时,创建自定义的FormsAuthenticationTicket对象,它包含了用户信息。
2. 加密FormsAuthenticationTicket对象。
3. 创建登录Cookie,它将包含FormsAuthenticationTicket对象加密后的结果。
4. 在管线的早期阶段,读取登录Cookie,如果有,则解密。
5. 从解密后的FormsAuthenticationTicket对象中还原我们保存的用户信息。
6. 设置HttpContext.User为我们自定义的对象。
你要从中加什么。 自己的问题
用我自己的话说。
获取用户名密码 - 加密 - 生成对象 - 存入cookie
获取cookic - 解密 - 生成对象
核心对象是FormsAuthenticationTicket + FormsAuthentication
PS: 不要说我写的含糊。 这里学习笔记。 看原文看这里 猛击