MVC Forms LoginUrl is incorrect

18,467

Solution 1

This is a known issue. I had the same problem with my custom authorize attribute. I found the solution somewhere on the net, can't remember where. Just add this to appSettings in your web.config

<add key="loginUrl" value="~/Account/LogOn" />

Note: This works with MVC 3, I didn't try it with previous versions.

EDIT: Found it mentioned in release notes, but it seems that they've changed the setting name and forgot to update release notes for RTM version.

Solution 2

I ran into a similar problem sometime ago. After a few months I discovered the root of the problem: I had added a 'deployable dependency' on 'ASP.NET Web Pages with Razor Syntax'. This adds a reference to: WebMatrix.Data.dll This assembly has a class with a static constructor that does the following:

static FormsAuthenticationSettings()
{
 FormsAuthenticationSettings.LoginUrlKey = "loginUrl";
 FormsAuthenticationSettings.DefaultLoginUrl = "~/Account/Login";
} 

Check if you are referencing this dll.

Solution 3

frennky's answer helped me get to this. I needed all of these in my web.config:

<appSettings>
  <add key="loginUrl" value="~/Authentication/LogOn" />
</appSettings>

<system.web>
  <authentication mode="Forms">
    <forms loginUrl="~/Authentication/LogOn" timeout="2880"></forms>
   </authentication>
   <authorization>
     <deny users="?"/>
   </authorization>
</system.web>

Solution 4

To fix this problem, which still exist in MVC 3 you have to remove the WebMatrix.*.dll from _bin_deployableAssemblies and bin folders respectively.

Solution 5

Instead of this:

<appSettings>
  <add key="loginUrl" value="~/Authentication/LogOn" />
</appSettings>

You could use this:

<appSettings>
  <add key="PreserveLoginUrl" value="true" />
</appSettings>

It worked for me.

Share:
18,467
jrummell
Author by

jrummell

Microsoft web development; it's what I do. http://www.controlaltdevelopment.com/

Updated on June 04, 2022

Comments

  • jrummell
    jrummell almost 2 years

    I have an ASP.NET MVC 3 application with forms authentication. For some reason that I cannot see, the login redirect url is /Account/Login?ReturnUrl=%2fSecure%2fAction instead of /Account/LogOn?ReturnUrl=%2fSecure%2fAction. The difference is subtle, its using /Account/Login instead of /Account/LogOn.

    My web.config forms section is correct. Would else could possibly affect the login url??

    <authentication mode="Forms">
      <forms loginUrl="~/Account/LogOn" timeout="720" />
    </authentication>