Jeremy Wadsworth
My Contribution to the ASP.NET Community


Welcome   
Welcome to my personal website. Here you will find blog posts related to web development as well as personal related interests. If you're here for the source code to my Personal Web Site Kit, please register and you'll receive an email when your account has been approved.


Latest Blog Entries

 Saturday, February 24, 2007


Can’t sign out of website in ASP.NET 2.0 / Single Sign On    
This stupid issue has plagued me for a while now. I eventually figured out what the issue was related to, I just didn’t know how to fix it until recently. The issue I was running into was this:

A user types in “mydomain.com” into the address bar and then logs in, checking the “Remember Me” checkbox. The user comes back to the website on at a later date and is automatically logged in as they should be. Then they decide to logout but when they click the logout link at the top of the page, the page refreshes and they aren’t logged out.

I wasn’t sure what the issue was so I wrote a custom logout page that my Logout link directs the user to. The logout page just executes the following code.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 If User.Identity.IsAuthenticated = True Then
  Session.Abandon()
  FormsAuthentication.SignOut()
 End If
 Response.Redirect("~/Login.aspx")
End Sub


However, this created another issue. Sometimes the user would be directed to the Login page after the code was executed but would just see a blank login page. If they navigated back to the home page, they would still be logged in.

I discovered that if the user logged in when the address read http://mydomain.com/login.aspx, and then later tried to click logout when the address read http://www.mydomain.com/somepage.aspx, they are not logged out. So it appears that the forms authentication cookie is specific to whether www is present in the URL.

I was able to fix this issue by writing code to change the cookie that was being used to authenticate the user. However, I just found the correct way to fix this issue. If you do a lot of web development and frequently have to program around the forms security model, I highly recommend the following book. ASP.NET 2.0 Security, Membership, and Role Management (WROX) by Stefan Schackow.

After reading chapter 5 and the section “Passing Tickets across Applications”, the answer was clear. It’s really a Single Sign On or SSO issue. In the Forms section of the web.config I needed to specify the domain. Otherwise, when the cookie is issued it will use the domain that is showing in the address bar which may or may not contain the www depending on how the user typed it in. Here is the code.

<forms loginUrl="Login.aspx" protection="Validation" timeout="5000000" path="/" domain="mydomain.com" />

Adding the domain property to the Forms tag, causes the forms authentication ticket to be issued using the exact domain you specify. It will also use that domain setting when signing out and expiring the ticket. Using this setting will allow users to log in at http://mydomain.com and still be authenticated when transferring them to http://subdomain.mydomain.com.


jeremy at 11:16 AM | (8) Comments | Add a comment | Permalink





Commments
Nemi said...

Great catch, i've had this problem myself and now it is resolved...

Friday, March 16, 2007 3:53 AM

rhea said...

web config of sinlge sign on

Friday, May 11, 2007 3:04 AM

Namrata said...

In my case I've given the sign out link in various pages it is working properly in almost all pages. But on one page the link is not working, i.e. is page is not getting directed to login page but the same page gets refreshed. Please somebody help me.

Thursday, May 08, 2008 1:43 AM

omer said...

i am also having some problem with logout functionallity. after logout, user can go back by pressing the browser back button, although i set no-cache through server side as well in html head tags(in master page). any idea?

Monday, July 21, 2008 3:24 AM

mejo said...

you are awesome

Saturday, November 29, 2008 9:14 AM

shyam said...

most of people said about how to sign out,but alas!i didn't get any truly useful& genuine formula of sign out in asp.net.Anybody can help me to solve this problem by giving some really genuine code in asp.net(language c#)that works on asp.net?

Monday, April 05, 2010 9:19 AM

hosting said...

Nice fix. I know it is a simple code problem and you figured it out and made a easy way to fix it.

Saturday, August 21, 2010 9:54 AM

Viagra said...

Nice answer

Monday, September 06, 2010 10:32 AM

  Leave a comment

Enter Your Name:
 
Enter your website
Security code from image below:  
Need Custom Controls Work or Training?
   


Join WebHost4Life.com






If you would like to help support this project, please click the button below to make a small donation.