Hi All,
Please help me in closing browser when clicked on log out button
string strClose = "";
Page.ClientScript.RegisterStartupScript(typeof(string), "clientScript", strClose);
i used above code but its not working, please help me.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Francis SusaimichaelRank #466 · 5.4K reputationPosted Nov 27, 2015, 9:00 AM EST
Mukesh KumarRank #119 · 18.2K reputationPosted Nov 27, 2015, 10:50 PM EST
There are a few things you can do to make sure the user is signed out when the browser is closed, but it depends on how you're setting the FormsAuthentication cookie:
Cookieless=True.FormsAuthentication.SetAuthCookieto set Persistence tofalsewindow.unload.Cookieless=Trueapproach:system.web> authentication mode="Forms"> forms loginUrl="/Account/Login" protection="All" cookieless="true" //set to true authentication> system.web>This appends the cookie value to the querystring in each request. The problem with this approach is it's not very secure and it messes with SEO. If a user sends anyone the URL they're using, that person can log in as the original user (probably not what you want). As far as 'messing with SEO', it causes the same page to look different to a googlebot based on what URL is passed in. Each QueryString change makes it a new URL, and if anyone uses this for posting a link; it will dilute the search results for a given actual URL.
FormsAuthenticationTicketApproachWhen you set an Authentication cookie for the user, set Persistent to
False.If you're doing this in the
FormsAuthentication.SetAuthCookie, this is default. If you use theFormsAuthenticationTicketclass, you have to specify the cookie expiration.FormsAuthentication.SetAuthCookie()ApproachBy default, if you don't set
persistent, the authentication cookie will expire at the end of the session (when the user closes the browser).JavaScript approach:
There are no foolproof methods; all you can do is set the cookie expiration date to before now and hope the user's browser co-operates. If you really, really, really, want the cookie gone, you can always try a JavaScript approach, but that won't work if the user has JavaScript disabled.
window.addEventListener('unload', function(event) { document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;'; });