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.
In Global.asax Application_Start, I have
DisplayModeProvider.Instance.Modes.Insert(1, new DefaultDisplayMode("iPhone")
{
ContextCondition = (context => context.GetOverriddenUserAgent().IndexOf("iPhone", StringComparison.OrdinalIgnoreCase) >= 0)
});
to register a display mode for an iPhone.
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.).
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).
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.
The problem occurs when adding forms authentication.
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 http://localhost:63087/Account/Login?ReturnUrl=%2f
. After logging in, I’m redirected back to the root and on to the correct display mode. All fine.
However, the URL doesn’t change. It stays as http://localhost:63087/Account/Login?ReturnUrl=%2f
. 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).
How can this problem be fixed?
How can an ASP.NET MVC 4 application with multiple display modes be made to work correctly with forms authentication?
Thanks.