• 可使用两种方法之一生成窗体身份验证 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);

    }

  • 相关阅读:
    数据库系列之T-SQL(系统内置函数)
    数据库系列之T-SQL(存储过程)
    数据库系列之T-SQL(作业与维护计划)
    数据库系列之T-SQL(触发器)
    数据库系列之T-SQL(事务)
    数据库系列之T-SQL(基础)
    数据库系列之查询(5)
    Nginx + Apache 公用80端口的配置
    客户端putty, xshell连接linux中vim的小键盘问题
    配置EPEL YUM源
  • 原文地址:https://www.cnblogs.com/ahuang1118/p/172542.html
Copyright © 2020-2023  润新知