Asp.net 如何在Closepage上注销

Asp.net 如何在Closepage上注销,asp.net,Asp.net,我需要在关闭页面或关闭浏览器上注销。。。 像往常一样 但默认情况下,ASP会员资格不会这样做 我刚离开网站时如何注销 (FormsAuthentication.SignOut(); HttpContext.Current.Session.Abund();) 顺便说一句,注销按钮出现问题。 下面是一个关于页面呈现的代码,用于检查user authorization=true I setup“authorezated panel with logout button” 但当我点击注销时 pr

我需要在关闭页面或关闭浏览器上注销。。。 像往常一样

但默认情况下,ASP会员资格不会这样做

我刚离开网站时如何注销

(FormsAuthentication.SignOut(); HttpContext.Current.Session.Abund();)

顺便说一句,注销按钮出现问题。 下面是一个关于页面呈现的代码,用于检查user authorization=true I setup“authorezated panel with logout button”

但当我点击注销时

    protected void Button2_Click(object sender, System.EventArgs e) //logout
    {
        if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
        {
            System.Web.Security.FormsAuthentication.SignOut();
            System.Web.HttpContext.Current.Session.Abandon();
            MultiView1.ActiveViewIndex = 1;
        }
    }
注销前的页面呈现,我看不到ActiveViewIndex=1:(


所以我需要在注销按钮上单击两次。奇怪。

当你关闭一个页面时,没有向服务器发送请求。只有一个javascript事件触发。所以你必须执行一个ajax请求来完成你需要的操作。但我不依赖于此。但是如果你真的需要在用户关闭页面时注销,那么这就是你要做的必须这样做。您可以这样做(如果使用jquery):


关于第二个问题,我建议将代码放在
Page\u PreRender()
Page\u Load()
中。

在第二个问题中,它会有所帮助。但是关于ajax请求……有没有示例?
    protected void Button2_Click(object sender, System.EventArgs e) //logout
    {
        if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
        {
            System.Web.Security.FormsAuthentication.SignOut();
            System.Web.HttpContext.Current.Session.Abandon();
            MultiView1.ActiveViewIndex = 1;
        }
    }
window.onunload = logOut;

function logOut() {
    $.get("[[url to a resource that logs you out]]");
}