FineUI 官方论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

本论坛已关闭(禁止注册、发帖和回复)
请移步 三石和他的朋友们

FineUI首页 WebForms - MVC & Core - JavaScript 常见问题 - QQ群 - 十周年征文活动

FineUI(开源版) 下载源代码 - 下载空项目 - 获取ExtJS - 文档 在线示例 - 版本更新 - 捐赠作者 - 教程

升级到 ASP.NET Core 3.1,快、快、快! 全新ASP.NET Core,比WebForms还简单! 欢迎加入【三石和他的朋友们】(基础版下载)

搜索
查看: 2975|回复: 0
打印 上一主题 下一主题

对Appbox的表单身份验证模式的认识理解

[复制链接]
跳转到指定楼层
楼主
发表于 2014-2-18 20:30:32 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 szjazz 于 2014-2-18 20:36 编辑

步骤一:设置验证模式及相关页面

在根目录下的web.config中加入:

  1. <font color="#333333"><font face="Verdana, Arial, Tahoma"><font style="font-size: 14px"><system.web>
  2. <authentication mode="Forms">
  3.       <forms name=".ASPXFORMSAUTH" loginUrl="~/default.aspx" timeout="120" defaultUrl="~/main.aspx" protection="All" path="/" />
  4.     </authentication>
  5. </system.web> </font></font></font>
复制代码


loginUrl:用户没有登录,跳转到的登录页面

defaultUrl:正确登录之后,在没有指向页的时候,弄人跳转的页面


步骤二:
拒绝匿名用户登录

  1. <font color="#333333"><font face="Verdana, Arial, Tahoma"><font style="font-size: 14px"><system.web>
  2.         <!--拒绝匿名用户访问此目录下的任何文件-->
  3.         <authorization>
  4.             <deny users="?"/>
  5.         </authorization>        
  6. </system.web> </font></font></font>
复制代码

deny users="?":表示禁止匿名用户访问web.config所在目录下的任何文件,只要你访问,都会自动跳转到Login.aspx登陆页面了,要求你先登录,否则别想看到页面。

注:如果根目录下的Web.config有该段代码,则表示整个系统都要求身份认证,适合类似OA的系统,如果是设计网站,则可以在后台管理目录admin下建立一个web.config,且包含步骤二的代码段,则所有的管理页面都要求验证,而前台给访问者看的页面则不需要了


步骤三:登录页面

在根目录下,创建Login.aspx登陆页面(可不是在admin目录下哦),加两个textbox控件一个botton控件,分别是用户名密码,和登陆按钮

双击登陆按钮,在其登陆方法里写上:

  1. <font color="#333333"><font face="Verdana, Arial, Tahoma"><font style="font-size: 14px">protected void btn_Login_Click(object sender, EventArgs e)
  2. {

  3.         if (TextBox1.Text == "admin" && TextBox2.Text == "fenghua17173")
  4.         {
  5.             //“通知”表单验证,该用户名已经通过身份验证
  6.             FormsAuthentication.RedirectFromLoginPage(TextBox1.Text, true);
  7.         }
  8.         else
  9.         {
  10.             Response.Write("<script>alert('账号或密码有误,登录失败!');</script>");
  11.         }
  12. } </font></font></font>
复制代码
现在配置文件目录下的所有页面,均已通过身份验证,得到了可访问的票据。

另外,本登录页面也要判断是否已登录,如有,则直接跳转至指定的默认页面,该段代码一般放在Page_Load事件里

  1.         protected void Page_Load(object sender, EventArgs e)
  2.         {
  3.             if (!IsPostBack)
  4.             {
  5.                  if (User.Identity.IsAuthenticated)
  6.                       Response.Redirect(FormsAuthentication.DefaultUrl);
  7.             }
  8.         }
复制代码

ok,这时你在login.aspx页面里填上账号密码,系统就会根据根你在根目录下web.config中配置的defaultUrl地址路径跳转过去。


步骤四:存储和获取验证用户的信息
一般情况下,验证通过后需要保留用户的信息,可以储存在Cookie:


  1. <font color="#000">/// <summary>
  2.         /// 创建表单验证的票证并存储在客户端Cookie中
  3.         /// </summary>
  4.         /// <param name="userName">当前登录用户名</param>
  5.         /// <param name="UserData">当前登录用户的信息数据,如:角色权限列表</param>
  6.         /// <param name="isPersistent">是否跨浏览器会话保存票证</param>
  7.         /// <param name="expiration">过期时间</param>
  8.         protected void CreateFormsAuthenticationTicket(string userName, string  UserData , bool isPersistent, DateTime expiration)
  9.         {
  10.             // 创建Forms身份验证票据
  11.             FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
  12.                 userName,                       // 与票证关联的用户名
  13.                 DateTime.Now,                   // 票证发出时间
  14.                 expiration,                     // 票证过期时间
  15.                 isPersistent,                   // 如果票证将存储在持久性 Cookie 中(跨浏览器会话保存),则为 true;否则为 false。
  16.                  UserData                          // 存储在票证中的用户特定的数据
  17.              );

  18.             // 对Forms身份验证票据进行加密,然后保存到客户端Cookie中
  19.             string hashTicket = FormsAuthentication.Encrypt(ticket);
  20.             HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashTicket);
  21.             cookie.HttpOnly = true;
  22.             // 1. 关闭浏览器即删除(Session Cookie):DateTime.MinValue
  23.             // 2. 指定时间后删除:大于 DateTime.Now 的某个值
  24.             // 3. 删除Cookie:小于 DateTime.Now 的某个值
  25.             if (isPersistent)
  26.             {
  27.                 cookie.Expires = expiration;
  28.             }
  29.             else
  30.             {
  31.                 cookie.Expires = DateTime.MinValue;
  32.             }
  33.             Response.Cookies.Add(cookie);
  34.         }</font>
复制代码

相应的,要取出储存的信息:


  1. <font color="#000">        /// <summary>
  2.         /// 当前登录用户名
  3.         /// </summary>
  4.         /// <returns></returns>
  5.         protected string GetIdentityName()
  6.         {
  7.             if (User.Identity.IsAuthenticated)
  8.             {
  9.                 return User.Identity.Name;
  10.             }
  11.             return String.Empty;
  12.         }</font>
复制代码

步骤五:退出系统,注销票证


最后一点:  

有登陆,当然别忘了注销,这个更简单:

在目录下的任何一个页面中,加一个注销button按钮,并在其方法下写入:

  1. <font color="#333333"><font face="Verdana, Arial, Tahoma"><font style="font-size: 14px">//退出系统,注销用户
  2. protected void btn_Logout_Click(object sender, EventArgs e)
  3. {
  4.         //删除用户票据
  5.         FormsAuthentication.SignOut();

  6.         //重新定向到登陆页面
  7.         FormsAuthentication.RedirectToLoginPage();

  8. }</font></font></font>
复制代码

好了,你已经知道如何配置authentication,完成基于表单的身份验证了。



您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|FineUI 官方论坛 ( 皖ICP备2021006167号-1 )

GMT+8, 2024-5-10 05:57 , Processed in 0.046960 second(s), 17 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表