Question

I want to remove the user authentication when the tab that had my webpage closed in any browser. And when user load my web page in new tab he should not see any his details until log in.

I used

 FormsAuthentication.SetAuthCookie("E|" + contct.EMailAddress1, false);

but clears only when complete browser close. I want to clear when tab closed.

What can I use in asp.net, C#

Was it helpful?

Solution

You can try deleting cookies using javascript onunload event.

<body onunload="deleteAllCookies()">

will call javascript function deleteAllCookies() on browser tab close, and in the function delete all cookies, will clear authentication

<script>
        function deleteAllCookies() {
            var cookies = document.cookie.split(";");

            for (var i = 0; i < cookies.length; i++) {
                var cookie = cookies[i];
                var eqPos = cookie.indexOf("=");
                var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
                document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
            }
        }
</script>

EDIT

Your question is to clear Authentication when tab closed in a browser. And you are using FormsAuthentication.SetAuthCookie as shown in question. That is why i suggested to delete cookies using javascript onunload event as in my answer . So it will clear Authentication when tab closed in a browser , won't consider another tab with different stages of the site (ie answer to the question).

If you want to maintain authentication on another tab with different stage of the site, You have to communicate between browser windows (and tabs too) using cookies . If you found another tab with same site don't clear cookies. You can do it in the javascript function. Hope this link will help you. Or try googling.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top