2015-06-03 95 views
2

我創建了一個主頁,其中有一個名爲退出的按鈕。如何使用該按鈕登出或註銷當前的登錄會話。如何使用html按鈕註銷asp.net?

這裏是按鈕的代碼:

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

任何幫助,不勝感激!

+2

只需提供註銷頁面的href值即可。 'HREF = 「Logout.aspx」'。 – Mairaj

回答

7

使用此註銷碼。

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

定時器滴答功能我必須把logout.aspx代碼放在後面?或者我必須在我的母版頁中添加計時器並添加此代碼? – Jilu

+2

不需要定時器。 – Mairaj

+0

定時器代碼代碼放入註銷頁面。 –

2

如果您使用窗體身份驗證,它可以在一個行完成:

FormsAuthentication.SignOut(); 

但我會後它可能添加:

FormsAuthentication.RedirectToLoginPage(); 
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"); 
    } 
}