zoukankan      html  css  js  c++  java
  • 利用form进行身份验证

      我们现在就把我们的一些流程说下:
           1.用户登录,在输入框中输入用户名和密码信息 (这里要规定,用户输入网址后,只能进入登陆界面)
           2.点击登录按钮后,到数据库中查询该用户是否存在 (点击按钮,后台对应的是:创建票据、发送票据、验证票据)
           3 如果存在,服务器端代码就创建一个身份验证的票据,保存在cookie中,然后发送到客户端的浏览器 (
           4.用户已经有了验证的cookie,那么就页面就跳转到用户之前请求的页面(票据都有保存时间,在规定的时间内随意跳转)

    前提:在数据库里,创建一个包含用户名、密码、角色的数据表(userid ,password,roles)

    第一步:创建一个登陆界面:Login.aspx, 上面有四个控件,(用户名、密码、提交按钮、提示信息)

     (1)前台程序:

    <form id="form1" runat="server">
      <div>
        <asp:Label ID="Label1" runat="server" Style="z-index: 100; left: 95px; position: absolute;
          top: 45px" Text="用户名:" Width="81px"></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server" Style="z-index: 101; left: 211px; position: absolute;
          top: 41px"></asp:TextBox>
        <asp:Label ID="Label2" runat="server" Style="z-index: 102; left: 97px; position: absolute;
          top: 88px" Text="密码:" Width="82px"></asp:Label>
        <asp:Label ID="Label3" runat="server" Style="z-index: 102; left: 168px; position: absolute;
          top: 134px" Width="198px"></asp:Label>
        <asp:TextBox ID="TextBox2" runat="server" Style="z-index: 103; left: 214px; position: absolute;
          top: 84px"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Style="z-index: 105; left: 320px; position: absolute;
          top: 160px" Text="Button" />

      </div>
    </form>

    (2)后台提交按钮程序:   验证成功,则创建表单、提交表单。 //验证不成功,则提示信息。

    protected void Button1_Click(object sender, EventArgs e)
    {
      string name = TBName.Text;
      string password = TBPassword.Text;

      if (name == "")
      {
        Label3.Text = "请输入用户名!";
        return;
      }
      else
      {
        if (password == "")
        {
          Label3.Text = "请输入密码!";
          return;
        }

      }


      string strcon = @"Data Source=www-4a3e4793e05SQLEXPRESS;Initial Catalog=shiyanku;Integrated Security=True";
      SqlConnection conn = new SqlConnection(strcon);//链接数据库
      string sqlstr = "select * from username where username='" + name + "' and password='" + password + "'";
      SqlCommand cmd = new SqlCommand();

      cmd.Connection = conn;
      cmd.CommandText = sqlstr;

      conn.Open();
      SqlDataReader dr = cmd.ExecuteReader();


      if (dr.Read())
      {
        //获取用户的角色
        string role = dr.GetValue(3).ToString();

        //创建身份验证票据
        FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, name, DateTime.Now, DateTime.Now.AddMinutes(40), false, role);

        //加密票据
        string encryptedTicket = FormsAuthentication.Encrypt(ticket);

        //创建新的cookie
        HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName);

        //把加密后的票据信息放入cookie
        cookie.Value = encryptedTicket;

        //把cookie添加到相应流中
        Response.Cookies.Add(cookie);

        //把cookie发送到客户端
        //Response.Redirect(FormsAuthentication.GetRedirectUrl(name,false),true);
        Response.Redirect(FormsAuthentication.GetRedirectUrl(name, true), true);

      }
      else
        Label3.Text = "您输入的用户名或密码不正确,请重新输入!";

        conn.Close();
     }

    第二步:创建验证表单程序

    创建全局应用类页面,Global.asax。一个程序只有一个这样的函数。

    void Application_AuthenticateRequest(object sender, EventArgs e)
    {
      HttpApplication app = (HttpApplication)sender;

      HttpCookie cookie=Request.Cookies[FormsAuthentication.FormsCookieName];


      if (cookie != null)
      {
        string encryptedTicket = cookie.Value;

        //解密cookie中的票据信息
        FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(encryptedTicket);

        //获取用户角色信息
        string[] role = ticket.UserData.Split(',');

        //创建用户标识
        FormsIdentity identity = new FormsIdentity(ticket);

        //创建用户的主题信息
        System.Security.Principal.GenericPrincipal user = new System.Security.Principal.GenericPrincipal(identity, role);

        app.Context.User = user;
            }
    }

    第三部:启动页面验证,添加在web.Config 中

    <configuration>

    <system.web>
    <!--
    设置 compilation debug="true" 将调试符号插入
    已编译的页面中。但由于这会
    影响性能,因此只在开发过程中将此值
    设置为 true。
    -->
    <compilation debug="true"/>
    <!--
    通过 <authentication> 节可以配置 ASP.NET 使用的
    安全身份验证模式,
    以标识传入的用户。
    -->

    <authentication mode="Forms">

    <forms name=".mycookie" path="/" loginUrl="Default3.aspx" defaultUrl="Default2.aspx" protection="All" timeout="80" />


    </authentication>

    <authorization>

    <allow users="*"/>
    <allow roles="admin"/>
    <deny users="?"/>

    </authorization>


    <!--
    如果在执行请求的过程中出现未处理的错误,
    则通过 <customErrors> 节可以配置相应的处理步骤。具体说来,
    开发人员通过该节可以配置
    要显示的 html 错误页
    以代替错误堆栈跟踪。

    <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
    <error statusCode="403" redirect="NoAccess.htm" />
    <error statusCode="404" redirect="FileNotFound.htm" />
    </customErrors>
    -->
    </system.web>

    <location path="check/FZRYCheck.aspx">
      <system.web>
        <authorization>
          <allow roles="admin,operator"/>
          <deny users="*"/>
          </authorization>
      </system.web>
    </location>


    <location path="input/FZRYInput.aspx">
      <system.web>
        <authorization>
          <allow roles="admin,operator"/>
          <deny users="*"/>
        </authorization>
      </system.web>
    </location>

    </configuration>

    说明:

    (1)票据验证的程序,要写在:<system.web>  </system.web> 之间。

    (2)初始配置:

      

    <compilation debug="false" />
    <!--
    通过 <authentication> 节可以配置 ASP.NET 使用的
    安全身份验证模式,
    以标识传入的用户。
    -->

     <authentication mode="Windows" />

    更改后的参数为:

    <compilation debug="true"/>

    <authentication mode="Forms">

      <forms name=".mycookie" path="/" loginUrl="Default3.aspx" defaultUrl="Default2.aspx" protection="All" timeout="80" />

    </authentication>

    <authorization>

      <allow users="*"/>            //允许所有用户            
      <allow roles="admin"/>      //允许admin  角色
      <deny users="?"/>           // 拒绝匿名访问

    </authorization>

    这部分写入的位置:

    <configuration>

       <system.web>

    。。。。。。。。。。。。。。。。。写在这个位置,allow,deny,验证的是所有的页面,如果页面有自己的单独要求,则见下面

      </system.web>

    </configuration>

    (3)对某个具体页面访问的控制

         写在默认的<system.web>  </system.web> 外面。自己重新建立一个框架

    <configuration>     ----系统自动编写的最外层的框架

      <system.web>    -----系统自动编写的默认的框架 

    .....................................................

     </system.web>

    <location path="input/FZRYInput.aspx">        -----自己添加的框架
      <system.web>
        <authorization>
          <allow roles="admin,operator"/>     //检查的顺序是从上到下
          <deny users="*"/>
        </authorization>
      </system.web>
    </location>

    。。。。。。。。。。。。。。。

    </configuration>

  • 相关阅读:
    撩妹技能 get,教你用 canvas 画一场流星雨
    git详细使用教程入门到精通(史上最全的git教程)
    Google Performance工具,你还不会用?Git走起。
    使用PIE.htc让万恶的IE内核浏览器IE678支持CSS3部分属性
    HTML编码规范
    Canvas制作的下雨动画
    Flex 布局教程:语法和实例
    CSS编码规范
    奇葩程序写的神一样的注释,被老板看见会不会开出呢?
    Vertical-Align你应该知道的一切
  • 原文地址:https://www.cnblogs.com/gongyu/p/3695814.html
Copyright © 2011-2022 走看看