Subscribe to Cache Cow        RSS Feed
-----

Redirecting Logins from Multiple Applications to a Portal in MVC 4

Icon Leave Comment
As I delve further into the inner workings of ASP.NET MVC, I have realized that I do not currently posses the skills necessary to perform all of the duties which I am assigned. That being said, there is also no task that I have been given which I have not been able to create a solution for. It may not be the "best" solution and it may not be the most efficient solution, but it works.

I had one of these moments come to me, where I was asked to do something that I had no idea how to do and I couldn't find much information about it on the internet. I was asked to singularize our login system. Up until recently, we had been recreating the same login code for each application that we deployed. This, of course, became very tedious when it came to updating the UI for our applications. If we added some information to the registration system, we had to go to each application and add the new elements to the UI.

All that being said, this is the solution I came up with which allows me to make any number of applications which will all go to the same "portal" application for anything dealing with membership. I am using VS 2012, Entity Framework 6 with SQL Server 2008, .net framework 4.5, and the stock SimpleMembership Views and Controllers that are created in the MVC 4 template.

The first thing you have to do when enabling a single sign on is to add a machine key to the config file. EVERY application which needs to have that single sign on enabled will have to have the EXACT SAME key. See this article Single Sign On (SSO)

Once you have SSO enabled, you can go into the account controller and modify it so that each of the membership ActionResults will redirect the user to your portal application Here is an example of the Login action:
[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
     var r = SanitizeURL(returnUrl);

     return Redirect(RedirectHelper.LoginPage() + "?returnUrl=" + r);        
}



For my applications, i created a static class to help with redirecting my actions. The SanitizeUrl method is going to differ depending on your needs. Some people use the RedirectToLocal method, but that didn't work for me. Basically, you just have to make sure that the return url that is passed (since it will be visible AND MODIFIABLE in the address bar) doesn't redirect the user to a malicious site. Since this is for my companies intranet, I make sure that the redirect is within our library of known intranet URL's.

You must also provide an action for logging out, but that is fairly self explanatory. At this point, all of your applications will redirect to the "portal" application for logging in and out. The one caveat to this is that all applications that use the single sign on must be in the same domain. If you change domains (i.e. http://mySite1/app1 to http://mySite2/app2), you will have to do some extra work to implement SSO.

I hope this has helped some of you to accomplish your SSO dreams! Cheers and happy coding!

0 Comments On This Entry

 

Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

January 2018

S M T W T F S
 123456
78910111213
14151617181920
2122 23 24252627
28293031   

Recent Entries

Search My Blog

0 user(s) viewing

0 Guests
0 member(s)
0 anonymous member(s)

Categories