Why does my site redirect back to the Login screen

2019-04-30 05:43发布

问题:

I'm upgrading an ASP.Net MVC4 site to MVC5, and implementing the new OWIN Authentication methods in the process.

I've copied over the action methods from the Account controller on a blank MVC5 project.

The problem is, when I click an external provider button (e.g. Google) I just get redirected back to the login page again. The second time I click it, I do get taken to the Google account page, but then the browser gets redirected to the Account/External login page.

What's going on?

回答1:

Check the <authentication> element in your web.config file. It is probably still saying

<authentication mode="Forms">

Changing it to <authentication mode="None"> should fix the problem.

For good measure, remove the FormsAuthentication module from your webserver modules section:

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

The problem is that the FormsAuthentication module looks out for any responses with a 401 (Unauthorised) code and changes them into a redirect request to your login page.



回答2:

I had the same issue and Samuel Jack's answer did help me, but I had to make a slight change.

For me, adding the

<remove name="FormsAuthentication" /> 

under the "handlers" section did not work. Instead, I had to add it under the "modules" section as below and the issue was resolved.

Here is how my web.config looks.

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

Note that in my case, there already was a

<remove name="FormsAuthenticationModule" />

in my web.config but that did not help solve the issue. I don't know how it got there so I left it alone.

Hope this helps.



回答3:

I had this exact problem for 1 day straight, but sometimes I have some bad days and my attention to details is low.

But my problem was a bit awkward. My external logins were fine for like few months and suddenly stopped working and I didn't touched that code at all however my webconfig was...

 <location path="something/somethingelse">
     //other stuff that made it hard to see because is pretty long
   <system.webServer>
      <modules>
         <remove name="FormsAuthenticationModule" />
         <remove name="FormsAuthentication" />
      </modules>
   </system.webServer>
 </location>

Luckily I figured this out before posting a question I was about to write and made me see that location tag.

I have no idea why it worked in the first place then got broken later. I haven't updated any reference, did literally nothing to this for weeks.