如何在asp.net中使用html按钮注销

本文关键字:html 按钮 注销 asp net | 更新日期: 2023-09-27 17:52:44

我创建了一个主页,其中有一个名为sign out的按钮。如何使用该按钮从当前登录会话登出或注销?

按钮代码:

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

任何帮助都非常感谢!

如何在asp.net中使用html按钮注销

使用此注销代码。

<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");
    }

如果您正在使用表单身份验证,它可以在一行中完成:

FormsAuthentication.SignOut();

不过我可能会在后面加上:

FormsAuthentication.RedirectToLoginPage();

你可以这样做:

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");
    }
}