Blog of Chat

March 21, 2008

Fix ReturnUrl When Sharing Forms Authentication with Multiple Web Applications

Also see: Introducing Microsoft Tagspace

Scenario: You have two web applications www.mydomain.com and login.mydomain.com. The login site provides a centralized login application and www contains any number of web applications that should use the auth ticket issued by the login site.

The auth ticket can be setup to be shared across the two 3rd level domains no problem. The problem with this setup is that when the user requests a page on www and gets redirected to login the ReturnUrl query string parameter contains a relative path. As far as I know there are not any extensibility points on the FormsAuthenication or FormsAuthenticationModule classes that you can use to fix this. A quick and dirty fix is to use the EndRequest event in your global.asax like this:

 

 1:  protected void Application_EndRequest(object sender, EventArgs e)
 2:  {
 3:  string redirectUrl = this.Response.RedirectLocation;
 4:  if (!string.IsNullOrEmpty(redirectUrl))
 5:  {
 6:  this.Response.RedirectLocation = Regex.Replace(redirectUrl, “ReturnUrl=(?’url’.*)”, delegate(Match m)
 7:  {
 8:  string url = HttpUtility.UrlDecode(m.Groups[“url”].Value);

Also see: Scott Guthrie presents at NDDNUG

Also see: New posting on MSDN about script leak patterns and how to fix them for anyone that builds dynamic web apps.

Also see: Introducing Microsoft Tagspace

Also see: Java Concurrency, another series on its issues

 9:  Uri u = new Uri(this.Request.Url, url);
 10:  return string.Format(“ReturnUrl={0}”, HttpUtility.UrlEncode(u.ToString()));
 11:  }, RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
 12:  }
 13:  }

The basic idea is to intercept the redirect and process the returnurl query string parameter with a regex. This could also be wrapped up in it’s own HttpModule. It’s kind of cheezy I know but it seems to work.

Developing Customer Relationship Management Solutions. Web, e-Commerce, Database Design and Software Development.

Also see: ASP.NET MVC in CodePlex and Extensible Unit Testing

Also see: New posting on MSDN about script leak patterns and how to fix them for anyone that builds dynamic web apps.

Also see: Stand alone FXCop download

Also see: Dare Obasanjo on C# Anonymous Types

eight=”1″>
http://weblogs.asp.net/dfindley/archive/2007/02/06/fix-returnurl-when-sharing-forms-authentication-with-multiple-web-applications.aspx

Comments »

The URI to TrackBack this entry is: http://spacechat.blogsome.com/2008/03/21/fix-returnurl-when-sharing-forms-authentication-with-multiple-web-applications-2/trackback/

No comments yet.

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>



Anti-spam measure: please retype the above text into the box provided.

Get free blog up and running in minutes with Blogsome
Theme designed by Jay of onefinejay.com