Click here to Skip to main content
Click here to Skip to main content

Single Sign On between sub domains : Forms Authentication

By , 18 Aug 2011
 

Today, I am going to discuss one of the feature, that I was working last few days and spent sleepless nights at office and Home as well. Here I am going to discuss Single Sign On (SSO) feature and every other developer implements SSO on some or other day. 

So Actually I was having two applications. The requirement was like, user logins from one application and on clicking a link it is navigated to another application. So when user clicks on the link it redirected to application2, it checks, whether the user is authenticated or not. If authenticated it can access the application2 else get redirected to first application for Authentication, And once authenticated again directly reaches the second application. 

Here my both application were Web application and form Authentication is used to authenticate the user. I had to devise some solution to implement SSO. I didn’t want to use the authentication code written on both applications so used first application only for Authentication. I thought of using the same authentication cookie that was created by First Application after authentication. And implemented this. 

It worked very well. I was able to read the authentication cookie in another application and wrote the logic accordingly. I deployed both applications on my local machine and Test QA Server and it worked like a charm. 

But… as soon as my application went actual test environment it started barfing. Nothing was working. No SSO.. Nothing.. 

Actually, what happened both the application were deployed on different web server in different domain. Like my first application has URL like app1.mydomain.com and another one app2.mydomain.com  

As I was not aware earlier I didn’t keep it in mind. As we know by default cookie is limited to domain. I was not able to access auth cookie in application2 as it was in separate domain. After some research on Internet, I happened to make the changes in auth cookie and updated the domain property as I found solutions on Google. The code was like 

System.Web.HttpCookie MyCookie =
       System.Web.Security.FormsAuthentication.GetAuthCookie(User.Identity.Name.ToString(),
                                                             false);
MyCookie.Domain = “.mydomain.com”;//the second level domain name
Response.AppendCookie(MyCookie); 

You can find it easily on internet. But the actual nightmare started after this only. Users were not able to logout. It kept looping as I checked in Fiddler and finally redirection error. Started searching on internet and found lots of people have problem but no thread ended with proper solution. I found at some places the auth cookie doesn’t get deleted if domain is set. Some workaround were there, like deleting the auth cookie manually when user logs out. But this also didn’t work. And ultimately did not find any solution on internet. 

So I did some brainstorming. Actually there is a method provided by as 

FormsAuthentication.SignOut()

which is supposed to logout the user once called but it was not doing it. Actually as suggested over internet I set the domain for Auth cookie. Which was creating some problem? FormAutetication itself provides a way to set the domain for cookie. This is a static property of the FormAuthentication Class. But this is read only. One need to set it in web.config file and it will also be easy you can change the domain whenever you want. So one can set it as 

<forms name=".ASPXAUTH" loginUrl="Login/" protection="Validation" timeout="120" path="/" domain=".mydomain.com"/>

After setting my application again started working perfectly fine. So wanted to share this to you all. One does not need to update the cookie manually as I did in first code sample above and later deleting it while logging out. It may not work. Just one need to set the domain at config file, one will be away from this endless problem and surfing the Internet. 

Hope this helps.

Thanks a lot 


License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Brij
Software Developer (Senior)
India India
Member
Brij is a Microsoft MVP in ASP.NET/IIS Category and a passionate .NET developer. Having around 5 years of experience in IT field, currently serving a MNC as a Sr. developer.
 
He is a very passionate .NET developer and have expertise over Web technologies like ASP.NET 2.0/3.5/4.0, jQuery, JSON, Javascript, IIS and related technologies. He is also a Exchange Server (EWS) Specialist. He has great experience in design patterns and N-Tier Architecture.
 
He is also certified as Microsoft Certified Technologies Specialist-ASP.NET and Microsoft Certified Technologies Specialist-WCF in .NET 4.0. He has also received several awards at various forums and his various articles got listed as "Article of the day" at ASP.NET Microsoft Official Website www.asp.net.
 
He has done MCA from NIT Durgapur and completed his graduation from Lucknow University.
 
Learning new technologies and sharing knowledge excites him most. Blogging, solving problems at various forums, helping people, keeps him busy entire day.


Visit his Blog: Brij's arena of .NET
 
Area of Expertise :
C#, ASP.NET 2.0,3.5,4.0, AJAX, JQuery, JSON, XML, XSLT, ADO.Net, WCF, Active Directory, Exchange Server 2007 (EWS), Java script, Web Services ,Win services, DotnetNuke, WSS 3.0,Sharepoint Designer, SQL Server 2000/2005/2008

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionI think if you set it manually (and not at web.config)...memberAnkur\m/23 Apr '12 - 3:04 
...you will have to set the Expires property of HttpCookie after you sign out using FormsAuthentication.SignOut();
Something like
cookie.Expires = DateTime.Now.AddDays(-1);
This usually ensures that the cookie expires and user will be eventually logged out.
 
I haven't tested this solution but I think this should work. Will try to test it and let you know.


..Go Green..

QuestionUseful article.memberBinhNhi0910 Feb '12 - 6:03 
Thanks.
AnswerRe: Useful article.mentorBrij11 Feb '12 - 22:34 
Thanks a lot
Cheers!!
Brij
Microsoft MVP ASP.NET/IIS
Visit my Blog: http://brijbhushan.net

GeneralMy vote of 5memberB Madan28 Dec '11 - 20:53 
For His Explanation and sharing
GeneralRe: My vote of 5mentorBrij23 Apr '12 - 5:46 
Thanks
Cheers!!
Brij
Microsoft MVP ASP.NET/IIS
Visit my Blog: http://brijbhushan.net

GeneralMy vote of 1memberSeanlm767 Oct '11 - 9:31 
waste of time reading this article.
GeneralRe: My vote of 1mvpBrij17 Oct '11 - 6:08 
Seanlm76 wrote:
waste of time reading this article.

Then why did you read? Didn't you read the topic first?
Anyways Just checked your profile, only 6 messages and all of "My vote of 1".
Cheers!!
Brij
Microsoft MVP ASP.NET/IIS
Visit my Blog: http://brijbhushan.net

GeneralMy vote of 5mvp_ Kunal Chowdhury _30 Aug '11 - 7:48 
That's a good topic that you covered here. 5ed Thumbs Up | :thumbsup:
GeneralRe: My vote of 5mvpBrij1 Sep '11 - 21:24 
Thanks Kunal!!
Cheers!!
Brij
Visit my Blog: http://brijbhushan.net

GeneralMy vote of 5memberJamesWittHurst22 Aug '11 - 12:22 
You put some good thought into your article and provided very useful information. I appreciate that.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 18 Aug 2011
Article Copyright 2011 by Brij
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid