【问题标题】:How to logout in asp.net using a html button?如何使用 html 按钮在 asp.net 中注销?
【发布时间】:2015-08-17 03:10:43
【问题描述】:

我创建了一个母版页,其中有一个名为“退出”的按钮。如何使用该按钮退出当前登录会话。

这里是按钮代码:

<a href="#" class="btn btn-default btn-flat">Sign out</a>

非常感谢任何帮助!

【问题讨论】:

  • 只要给出注销页面的href值即可。 href="Logout.aspx".

标签: c# html asp.net master-pages


【解决方案1】:

你可以用这个:

logout.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="logout.aspx.cs" Inherits="logout" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Logout</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Logout successful."></asp:Label>
        <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/index.aspx">
            You will redirect in 5 seconds. If you didnt, click here to redirect.</asp:HyperLink>
    </div>
    </form>
</body>
</html> 

logout.aspx.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class logout : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Session.Abandon();
        Session.Clear();
        Session.RemoveAll();
        Response.AppendHeader("Refresh", "5;url=index.aspx");
    }
}

【讨论】:

    【解决方案2】:

    如果您使用的是表单认证,则可以一行完成:

    FormsAuthentication.SignOut();
    

    虽然我可能会在它之后添加:

    FormsAuthentication.RedirectToLoginPage();
    

    【讨论】:

      【解决方案3】:

      使用此注销代码。

      <a href="LogOut.aspx" class="btn btn-default btn-flat">Sign out</a>
      

      LogOut.aspx

      <form id="form1" runat="server">
          <asp:Label ID="Label1" Text="Loggin Out Please Wait" runat="server" />
          <asp:ScriptManager ID="ScriptManager1" runat="server">
          </asp:ScriptManager>
          <div>
              <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                  <ContentTemplate>
                      <asp:Timer ID="Timer1" runat="server" Interval="1000" OnTick="Timer1_Tick">
                      </asp:Timer>
                  </ContentTemplate>
              </asp:UpdatePanel>
          </div>
          </form>
      

      Logout.aspx.cs

      protected void Timer1_Tick(object sender, EventArgs e)
          {
              Session.Clear();
              Session.Abandon();
              Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
              Response.Cache.SetCacheability(HttpCacheability.NoCache);
              Response.Cache.SetNoStore();
      
              try
              {
                  Session.Abandon();
                  FormsAuthentication.SignOut();
                  Response.Cache.SetCacheability(HttpCacheability.NoCache);
                  Response.Buffer = true;
                  Response.ExpiresAbsolute = DateTime.Now.AddDays(-1d);
                  Response.Expires = -1000;
                  Response.CacheControl = "no-cache";
                  //Response.Redirect("login.aspx", true);
              }
              catch (Exception ex)
              {
                  Response.Write(ex.Message);
              }
              Response.Redirect("~/Login.aspx");
          }
      

      【讨论】:

      • Timer Tick 函数我要在logout.aspx代码后面放?或者我必须在我的母版页中添加计时器并添加此代码?
      • 不需要 Timer 。
      • Timer Tick Code 放入注销页面。
      • 母版页只在注销页面添加链接所有代码都是注销页面。
      • 仅在您的页面注销 5 秒且所有会话过期后才使用计时器滴答声。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-21
      • 2015-06-10
      • 1970-01-01
      • 2013-03-25
      • 2012-01-20
      • 1970-01-01
      相关资源
      最近更新 更多