I need to logout on close page or on close browser ... as usual ...
but by default ASP Membership wont do it ...
How to make logout when I just leave my site
(FormsAuthentication.SignOut(); HttpContext.Current.Session.Abandon();)
btw got problem on logout button. here is a code on page render to check if user authorization=true I setup "authorezated panel with logout button"
protected void Page_PreRender()
{
if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
{
Label1.Text = System.Web.HttpContext.Current.User.Identity.Name;
MultiView1.ActiveViewIndex = 0;
}
else
{
MultiView1.ActiveViewIndex = 1;
}
}
but when I click logout
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.Curr开发者_JS百科ent.Session.Abandon();
MultiView1.ActiveViewIndex = 1;
}
}
Page rendering before logout and I can't see ActiveViewIndex = 1 :(
So I need to click twice on logout button . weird.
There is no request sent to the server when you close a page. There is only a javascript event that fires. So you would have to do a ajax request to do what you need. But I wouldn't depend on that. But if you really need to sign out your user when he closes the page, then that is what you have to do. You can do something like this (if you use jquery):
window.onunload = logOut;
function logOut() {
$.get("[[url to a resource that logs you out]]");
}
About the second problem, I would suggest put the code in your Page_PreRender()
in Page_Load()
instead.
精彩评论