New Asp.Net MVC5 project produces an infinite loop to login page

asp.netasp.net MvcVisual Studio-2013asp.net Mvc-5

asp.net Problem Overview


I am creating a brand new projet with Visual Studio 2013, I choose Asp.Net MVC and the framework 4.5.1 The project is created, then, I do nothing else than F5 to start the default web page. Unfortunately, it produces a redirect to the login page which is redirecting into the login page too. Here is a short version of the url I have in the browser:

http://localhost:5285/Account/Login?ReturnUrl=%2FAccount%2FLogin%3FReturnUrl%3D%252FAccount%252FLogin%253FReturnUrl%253D%25252FAccount%25252FLogin%25253FReturnUrl%25253D%2525252FAccount%2525252FLogin%2525253FReturnUrl%2525253D%252525252FAccount%252525252FLogin%252525253FReturnUrl%252525253D%25252525252FAccount%25252525252FLogin%25252525253FReturnUrl%25252525253D%2525252525252FAccount%2525252525252FLogin%2525252525253FReturnUrl%2525252525253D%252525252525

I do not have any error in the Event Viewer. But in the screen I see :

> "HTTP Error 404.15 - Not Found The request filtering module is > configured to deny a request where the query string is too long."

The website is running with the default setting in IIS Express. How can I fix this problem? I am guessing something is wrong with my Visual Studio 2013?

##Edit It works if I create a brand new website and I host it in IIS. But if I create a new website (without modifying anything) and just hit play (which start IIS Express by default), it doesn't.

##Edit 2

I have deleted every websites in the Documents\IISExpress\config\applicationhost.config. I have recompiled everything, and it created this entry :

    <siteDefaults>
        <logFile logFormat="W3C" directory="%IIS_USER_HOME%\Logs" />
        <traceFailedRequestsLogging directory="%IIS_USER_HOME%\TraceLogFiles" enabled="true" maxLogFileSizeKB="1024" />
    </siteDefaults>
    <applicationDefaults applicationPool="Clr4IntegratedAppPool" />
    <virtualDirectoryDefaults allowSubDirConfig="true" />
</sites>

I am still getting the error with IIS Express, not with IIS.

asp.net Solutions


Solution 1 - asp.net

Highlight the project in Visual Studio

Open the 'Properties' panel on the right (or press F4)

Set 'Windows Authentication' to 'Disabled'

Set 'Anonymous Authentication' to 'Enabled'

Solution 2 - asp.net

You are missing [AllowAnonymous] attribute on login action.

[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
    // code....
}

2nd possibility, specific to IIS Express only: is that, if you created same default WebApplication1 project multiple times, playing with different authentication settings, IIS Express stored additional authentication settings in it's configuration file. Something like:

    <location path="WebApplication1">
        <system.webServer>
            <security>
                <authentication>
                    <windowsAuthentication enabled="true" />
                    <anonymousAuthentication enabled="false" />
                </authentication>
            </security>
        </system.webServer>
    </location>
</configuration>

Configurations are in user's Documents folder Documents\IISExpress\config\, and you should look for:

applicationhost.config

Then just delete xml node <location path="WebApplication1"> mentioned above.


Update for VS 2015+

If you're using Visual Studio 2015 or higher, check this path for the config file: $(solutionDir)\.vs\config\applicationhost.config

Each solution will have its own config file.

Solution 3 - asp.net

This issue is because of the authentication mode selected(by default) by the MVC 5 Template, which triggers the ReturnUrl Style of redirection that might lead to an infinite loop if not configured correctly.

To disable OWIN startup discovery,add this key to your webconfig file.

<add key="owin:AutomaticAppStartup" value="false"/>

Solution 4 - asp.net

I had to remove (Source Link):

<authorization>
  <deny users="?" />
</authorization>

Solution 5 - asp.net

I know I may be late, and this is not directly for the OP's question. But if anyone in the future come here, one more check about AllowAnonymous and Authorize attribute is that, you have to check all child actions too.

For example, I had my Layout (which the Login page also use) that call 2 child actions for breadcrumbs and sidebar, and they did not have AllowAnonymous attribute (the Controller had Authorize attribute).

Hope this help.

Solution 6 - asp.net

In IIS, Select you website and check for Authentication, If you are using Forms Authentication then -

  1. Set 'Windows Authentication' to 'Disabled' ,
  2. Set 'Anonymous Authentication' to 'Enabled'
  3. Set 'Forms Authentication' to 'Enabled'

Solution 7 - asp.net

ASP.Net MVC 5 template adds Microsoft.Owin and related libraries to the project. Since Owin infrastructure doesn't require Forms Authentication, the template also introduces the following key in web.config.

<system.webServer>
  <modules>
    <remove name="FormsAuthentication" />
  </modules>
</system.webServer>

Presence of this key could be a reason for undesirable looping back to Login page. Commenting it may help fix the problem for some people.

Solution 8 - asp.net

I faced the same problem because my MVC project was configured for .Net 4.5 but I was using .Net 4.0 as my application pool in IIS. Switched it to .Net 4.5 application pool and the problem was fixed. I hope this helps some one else!

Solution 9 - asp.net

TL:DR? Do not call a protected web API (any web API which requires Authorization) from an authorization page such as ~/Account/Login (which, by itself, does NOT do this.). If you do you will enter into an infinite redirect loop on the server-side.

Cause

I found that the culprit was, indirectly, AccountController::Authorize and the fact that AccountController is decorated with [Authorize].

The root cause was Sammy() being called from HomeViewModel() (Line 6 of home.viewmodel.js), which was accessing a "protected web API". This was being done for /Account/Login, which resulted in /Account/Login redirecting to itself.

Confirmation

You can confirm this is the cause of your problem through several methods:

  1. Decorate AccountController::Authorize with [AllowAnonymous]
  2. Comment out the Sammy() calls made during viewmodel construction.
Solution

The solution was to only emit the app bundle (a.k.a "~/bundles/app") for views which already required authorization. To my knowledge /Account/ views are classic MVC-based views, and are not part of the app datamodel/viewmodel, but I had mistakenly moved the bundle Scripts.Render(@"~/bundles/app") call into _Layout.cshtml (causing protected web API calls to be made for all MVC views, including /Account/.)

Solution 10 - asp.net

in my case: in my _layout.cshtml, i use Html.Action to call Action from Authorize Controller: ex: Html.Action("Count", "Product") -> loop error

fix: decorate by [AllowAnonymous] attribute in that Action (or remove these Html helper from _layout)

Solution 11 - asp.net

I just dealt with this issue for hours on end.

For me, it was in the Startup.Auth.cs file.

This code, when commented out, stopped the redirect loop.

        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login")
        });

Solution 12 - asp.net

Please be aware that this is potentially harmful advice, it's rarely a good idea to modify an applicationhost config file directly, there are usually tools that will do this for you, safely (for example, from within Visual Studio.) Before proceeding, be sure to create a backup copy of this file in the event your IIS Express becomes trashed.

To fix this problem, I took the default IIS configuration file located here :

C:\Windows\System32\inetsrv\config\applicationHost.config

To my document

%userprofile%\documents\iisexpress\config\applicationhost.config

And it worked.

This was because I had some Windows Authentification set and not the anonymous account.

Solution 13 - asp.net

Make sure you have no actions in pipeline that have authorize attribute. In my case, my layout had navigation menu controller which was missing allowAnonymous attribute.

Solution 14 - asp.net

I solved the same problem thanks to this accepted answer: <https://stackoverflow.com/questions/23107030/asp-net-login-redirect-loop-when-user-not-in-role>;.

It is possible that the controller containing Login action is decorated with an AuthorizeAttribute (even a custom one) while the login action is not decorated with AllowAnonymous attribute. Removing AuthorizeAttribute from the controller and adding AllowAnonymous to login action may be a possible solution.

Solution 15 - asp.net

These answers are more or less pieces of the same puzzle; I'll try to put everything in one place. Problem that OP described hit my application the moment I implemented the OWIN pipeline and AspNET Identity.

So let's see how to fix it...

  1. OWIN Startup

I guess you need it, because if you don't, then you don't need authentication, and I guess you do. Except it you're using some old-style authentication, and I guess you don't. So, don't remove either the OWIN startup attribute...

[assembly: OwinStartupAttribute(typeof(YourApp.Probably_App_Start.SomethingLikeAuthConfig))]

...or the configuration line...

<add key="owin:AppStartup" value="YourApp.Probably_App_Start.SomethingLikeAuthConfig" />

2. Access restriction on controllers

Now we cleared this up, you need the authentication. This means either each of your controller needs the [Authorize] attribute, or you can do the same to all controllers in one place by registering the thing globally (e.g. in RegisterGlobalFilters(), add line filter.Add(new AuthorizeAttribute())). In the former case (when securing each controller separately) skip this part, just go to the next one. In the latter case all of your controllers will be secured against unauthorized acces, so you need an entry point for that authorization - unprotected Login() action. Just add...

[AllowAnonymous]

...and you should be good.

  1. OWIN cookie configuration

When your user logs in, his browser stores encrypted (hopefully!) cookie in order to simplify things for the system. So, you need cookie - don't delete the line that says UseCookieAuthentication.

  1. What you really have to do is turn off the IIS integrated authentication mechanism for your web application. This means switching off Windows Authentication (Disabled) and enable letting any user in, at least as long as IIS Express is now concerned, by setting Anonymous Authentication (Enabled).

When you start your web site, this will in turn copy these settings into IIS Express configuration (applicationhost.config), and there you should see these two lines:

<windowsAuthentication enabled="false" />
<anonymousAuthentication enabled="true" />

5. You might have the authorization config in your web.config that says deny users="?". It means the authorization subsystem is instructed to prevent anonymous users from entering. With OWIN, this still works as designed. You either have to remove this, or make your anonymous user able to access the Login page by using something like...

`<location path="Account/Login">
  <system.web>
    <authorization>
      <allow users="*" />
    </authorization>
  </system.web>
</location>`

HTH

Solution 16 - asp.net

I had similar issues where it was in an infinite loop when calling back to the website locally. It turns out that when debugging locally it was redirecting the ports. I updated port numbers in the project properties screen but left the Azure definition the same in the cloud project and everything started to work as expected.

Solution 17 - asp.net

I had the same issue with my Asp.Net MVC 4 project. I resolved it by going to Startup.cs and commenting out the line for ConfigureAuth(app)

    public void Configuration(IAppBuilder app)
    {
        //ConfigureAuth(app);
    }

I also made sure that I had Windows Authentication enabled in IIS for my project, and all other authentication options disabled.

Solution 18 - asp.net

For me, this turned out to be caused by my LoginViewModel containing references to translation resources files, apparently being protected by authentication. I removed those references, and the problem was solved.

Solution 19 - asp.net

For me, removing the following block fixed it:

<authorization>
  <deny users="?" />
  <allow users="*" />
</authorization>

Assume

<authentication mode="None" />

Solution 20 - asp.net

in my case it was a very wired problem , i decorated the home controller by non existent role. so it causes a redirection loop.

Solution 21 - asp.net

Go to to your applicationhost.config file and set anonymousauthentication = "true"

<authentication>

            <anonymousAuthentication enabled="true" userName="" />
            <windowsAuthentication enabled="true">
                <providers>
                    <add value="Negotiate" />
                    <add value="NTLM" />
                </providers>
            </windowsAuthentication>

        </authentication>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPatrick DesjardinsView Question on Stackoverflow
Solution 1 - asp.netRobA2345View Answer on Stackoverflow
Solution 2 - asp.netNenadView Answer on Stackoverflow
Solution 3 - asp.netuser3573341View Answer on Stackoverflow
Solution 4 - asp.netAlkemicharView Answer on Stackoverflow
Solution 5 - asp.netLuke VoView Answer on Stackoverflow
Solution 6 - asp.netSanjay SharmaView Answer on Stackoverflow
Solution 7 - asp.netA is AView Answer on Stackoverflow
Solution 8 - asp.netKamView Answer on Stackoverflow
Solution 9 - asp.netShaun WilsonView Answer on Stackoverflow
Solution 10 - asp.netTienQuangView Answer on Stackoverflow
Solution 11 - asp.netNickView Answer on Stackoverflow
Solution 12 - asp.netPatrick DesjardinsView Answer on Stackoverflow
Solution 13 - asp.netKalyanView Answer on Stackoverflow
Solution 14 - asp.netsilviagreenView Answer on Stackoverflow
Solution 15 - asp.netOzrenTkalcecKrznaricView Answer on Stackoverflow
Solution 16 - asp.netSteve NewtonView Answer on Stackoverflow
Solution 17 - asp.netAndeeCView Answer on Stackoverflow
Solution 18 - asp.netMichaelCleverlyView Answer on Stackoverflow
Solution 19 - asp.netemraginsView Answer on Stackoverflow
Solution 20 - asp.netBaouche IqbalView Answer on Stackoverflow
Solution 21 - asp.netamenocalView Answer on Stackoverflow