1

TL/DR: I am experiencing an HTTP-302 redirect loop when trying to share an authentication cookie between an ASP.NET WebForms site and an ASP.NET MVC web site that are served on separate sub-domains.

Details

a.website.com - The existing WebForms site. Works fine.

b.website.com - The new MVC site I'm trying to integrate using the shared cookie.

  1. User reaches b.website.com and is not yet authenticated. They are redirected to a.website.com/Login.aspx.
  2. User enters their username/password.
  3. The user is redirected to the original desired page on b.website.com.
  4. b.website.com receives the authentication cookie in the request, but for some reason fails to see the user as authenticated. It sends them back to a.website.com/Login.aspx.
  5. a.website.com sees the cookie and realizes the user is authenticated, and redirects the user back to b.website.com.

Steps 4 and 5 repeat until the browser breaks the loop.

Setup

The setup follows the MSDN documentation for Configuring Forms Authentication Across Applications precisely: the two sites share machine key settings and Forms authentication settings. Both sites reside on the same server and IIS instance. Both sites force SSL. In the web.config file, the Membership and RoleManager settings are identical.

For a.website.com, which hosts the login page:

<httpCookies httpOnlyCookies="true" requireSSL="true" />    
<authentication mode="Forms">
<forms name="COOKIE_NAME"
         domain=".website.com"
         loginUrl="login.aspx" 
         defaultUrl="login.aspx"
         requireSSL="true"
         cookieless="UseCookies"
         protection="All"
         enableCrossAppRedirects="true"
         path="/"/>
</authentication>
<machineKey
  validationKey="((validation key))"
  decryptionKey="((decryption key))"
  validation="SHA1"
  decryption="AES" />

b.website.com is of course pretty similar, except the login URL for unauthorized users directs them to the login on site "a".

<httpCookies httpOnlyCookies="true" requireSSL="true" />
<authentication mode="Forms">
<forms name="COOKIE_NAME" 
   domain=".website.com" 
   loginUrl="https://a.website.com/login.aspx" 
   defaultUrl="login.aspx" 
   requireSSL="true" 
   cookieless="UseCookies" 
   protection="All" 
   enableCrossAppRedirects="true" 
   path="/"/> 
</authentication> 
<machineKey validationKey="((validation key))" 
        decryptionKey="((decryption key))" 
        validation="SHA1" 
        decryption="AES" />

Troubleshooting

This setup works as expected on my development machine and our test server (albeit with no domain setting and loginUrl set accordingly) where both web sites are running from the same domain, just different port numbers. For example, in local development, site "a" might run from https://development:44301/ and site "b" might run from https://development:44302/. However, on the production machine--where they are actually on different subdomains--I experience the redirect loop.

Using the browser web developer tools, I can see that the authorization cookie is being sent to b.website.com after the login redirect. Also on b.website.com, if I remove the [Authorize] attribute decorating the controller, the pages load as expected. I'm reasonably sure the issue is limited to how the MVC site is handling the authentication cookie in the limited subdomain scenario.

It sounds like the same issue was reported on SO here and here, but those users had not set enableCrossAppRedirects="true". That setting (which is enabled in my code) seems to be required for subdomain redirects, as I've already tried. This SO article details the setting a bit more and indicates the redirect should be done via SSL, which I am also doing.

What am I missing? How can I better debug the authentication failure that apparently occurs when site "b" receives but does not acknowledge the cookie?

Synctrex
  • 811
  • 1
  • 11
  • 19

1 Answers1

1

I finally found an answer, unmentioned in the MSDN documentation I referenced above. Many thanks to Steve Smith's 2-year old blog entry.

The older WebForms site targeted a previous release of .NET, so there is an additional compatibility mode string setting on the machineKey config to handle this. If both applications were the same version, this would be unnecessary.

To be absolutely clear for future searchers-- In the web.config of the older a.website.com WebForms site, targeting an earlier .NET release, I had used an ordinary machinekey setting:

<machineKey
 validationKey="((validation key))"
 decryptionKey="((decryption key))"
 validation="SHA1"
 decryption="AES" />

In the web.config of the newer MVC site, I had to also specify the compatibility mode to work with the older site's framework:

<machineKey
 compatibilityMode="Framework20SP2"
 validationKey="((validation key))"
 decryptionKey="((decryption key))"
 validation="SHA1"
 decryption="AES" />

Note that the compatibility mode string is NOT necessarily the targeted framework of the other app. Any targeted version between 2.0SP2 and 4.5 will need a setting of "Framework20SP2". Check the link above to make sure you're picking the right one!

With this in place (along with the matching forms settings in the original MSDN article), everything worked perfectly.

I foresee myself spending another day figuring out why my authentication is broken when we get around to upgrading the older site's targeted framework!

Synctrex
  • 811
  • 1
  • 11
  • 19