zoukankan      html  css  js  c++  java
  • 可使用两种方法之一生成窗体身份验证 Cookie,并将用户重定向到 cmdLogin_ServerClick 事件中的相应页。

    为两种情形都提供了示例代码。可根据需要使用其中的一个。

    调用 RedirectFromLoginPage 方法,以便自动生成窗体身份验证 Cookie,并将用户重定向到 cmdLogin_ServerClick 事件中的相应页:

    private void cmdLogin_ServerClick(object sender, System.EventArgs e)

    {

    if (ValidateUser(txtUserName.Value,txtUserPass.Value) )

        FormsAuthentication.RedirectFromLoginPage(txtUserName.Value,

            chkPersistCookie.Checked);

        else

            Response.Redirect("logon.aspx", true);

    }

    生成身份验证票证,对其进行加密,创建 Cookie,将其添加到响应中并重定向用户。这样,您就可以更好地控制 Cookie 的创建方式了。在本例中还可包括自定义数据和 FormsAuthenticationTicket

    private void cmdLogin_ServerClick(object sender, System.EventArgs e)

    {

       if (ValidateUser(txtUserName.Value,txtUserPass.Value) )

       {

          FormsAuthenticationTicket tkt;

          string cookiestr;

          HttpCookie ck;

          tkt = new FormsAuthenticationTicket(1, txtUserName.Value, DateTime.Now,

    DateTime.Now.AddMinutes(30), chkPersistCookie.Checked, "your custom data");

          cookiestr = FormsAuthentication.Encrypt(tkt);

          ck = new HttpCookie(FormsAuthentication.FormsCookieName, cookiestr);

          if (chkPersistCookie.Checked)

          ck.Expires=tkt.Expiration;

                ck.Path = FormsAuthentication.FormsCookiePath;

          Response.Cookies.Add(ck);

     

          string strRedirect;

          strRedirect = Request["ReturnUrl"];

          if (strRedirect==null)

                strRedirect = "default.aspx";

             Response.Redirect(strRedirect, true);

       }

       else

          Response.Redirect("logon.aspx", true);

    }

  • 相关阅读:
    Java基本开发环境搭建(适合第一次使用)
    在oracle中通过链接服务器(dblink)访问sql server
    C# 鼠标悬停在datagridview的某单元格,显示悬浮框效果
    经典SQL语句大全
    程序员为何要写技术博客?
    收缩SQL数据库日志
    利用脚本设置本机IP地址
    在SQL Server 2005中连接Oracle,完成查询、插入操作
    Centos 7 下 Corosync + Pacemaker + psc + HA-proxy 实现业务高可用
    Centos 7 下 Corosync + Pacemaker + psc 实现 httpd 服务高可用
  • 原文地址:https://www.cnblogs.com/ahuang1118/p/172542.html
Copyright © 2011-2022 走看看