[英]How to logout in asp.net using a html button?
I've created a masterpage where there is a button named sign out. 我创建了一个母版页,其中有一个名为sign out的按钮。 How can I use that button in order to sign out or logout from the current login session.
如何使用该按钮退出或退出当前登录会话。
Here is the button code: 这是按钮代码:
<a href="#" class="btn btn-default btn-flat">Sign out</a>
Any help is greatly appreciated! 任何帮助是极大的赞赏!
use this logout code. 使用此注销码。
<a href="LogOut.aspx" class="btn btn-default btn-flat">Sign out</a>
LogOut.aspx 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 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");
}
If you are using forms authentication, it can be done in one line: 如果您使用表单身份验证,则可以在一行中完成:
FormsAuthentication.SignOut();
though I would probably add after it: 虽然我可能会在它之后添加:
FormsAuthentication.RedirectToLoginPage();
You can use this: 你可以用这个:
logout.aspx: 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: 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");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.