title: Problems with ASP.NET MVC 4 Display Modes and Forms Authentication
body: <p>I have an ASP.NET MVC 4 application that uses forms authentication (I started with the ASP.NET MVC 4 mobile template).  I have also added display modes functionality to support mobile devices.  However, forms authentication doesn’t seem to work with these different display modes.</p>
  
  <p>In Global.asax Application_Start, I have</p>
  
  <pre><code>DisplayModeProvider.Instance.Modes.Insert(1, new DefaultDisplayMode("iPhone")
  {
      ContextCondition = (context =&gt; context.GetOverriddenUserAgent().IndexOf("iPhone", StringComparison.OrdinalIgnoreCase) &gt;= 0)
  });
  </code></pre>
  
  <p>to register a display mode for an iPhone.</p>
  
  <p>In ~/Views/Shared, I have _Layout.cshtml for desktop browsers and _Layout.iPhone.cshtml for iPhones.  In ~/Views/Home, I have Index.cshtml for desktop browsers and Index.iPhone.cshtml for iPhones.  In ~/Views/Account, I have the standard .cshtml files that come with the ASP.NET MVC 4 mobile web template (Index, Login, Register, etc.).</p>
  
  <p>In FilterConfig.cs RegisterGlobalFilters, I added filters.Add(new System.Web.Mvc.AuthorizeAttribute()); so that authentication is needed to navigate anywhere in the site (except the AccountController, which has [AllowAnonymous] on the Login and Register action methods).</p>
  
  <p>This is all basic stuff and set up correctly.  When navigating to the site in the browser (from both a desktop browser and an iPhone browser), the display modes settings/configuration works great and I’m taken to either the normal .cshtml files or the iPhone-specific files.</p>
  
  <p>The problem occurs when adding forms authentication.</p>
  
  <p>When I first go to the site, since I’m not authenticated, I’m redirected to the Login page (as expected).  The URL shows as <code>http://localhost:63087/Account/Login?ReturnUrl=%2f</code> .  After logging in, I’m redirected back to the root and on to the correct display mode.  All fine.</p>
  
  <p>However, the URL doesn’t change.  It stays as <code>http://localhost:63087/Account/Login?ReturnUrl=%2f</code> .  This is a problem.  Now any future navigation gets confused and sends me back to the Login page (even though I’ve already logged in and was authenticated).</p>
  
  <p>How can this problem be fixed?</p>
  
  <p>How can an ASP.NET MVC 4 application with multiple display modes be made to work correctly with forms authentication?</p>
  
  <p>Thanks.</p>