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

后端 未结 21 3173
粉色の甜心
粉色の甜心 2020-11-29 20:12

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 d

相关标签:
21条回答
  • 2020-11-29 20:46

    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'
    0 讨论(0)
  • 2020-11-29 20:46

    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/.)

    0 讨论(0)
  • 2020-11-29 20:46

    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.

    0 讨论(0)
  • 2020-11-29 20:50

    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")
            });
    
    0 讨论(0)
  • 2020-11-29 20:51

    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.

    0 讨论(0)
  • 2020-11-29 20:52

    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"/>
    
    0 讨论(0)
提交回复
热议问题