Nicht autorisierte Anfrage nicht umleiten nicht auf Login-Seite mit returnUrl Query-String-Parameter

StackOverflow https://stackoverflow.com/questions/4478570

Frage

Setup

In meinem MVC3 App wird MembersController dekoriert mit einem [autorisieren] Attribut.

MembersController hat eine Aktion namens MyPage. Aufgrund der Autorisieren Attribut auf dem Controller, MyPage kann nur von autorisierten Benutzern angefordert werden.

Problem

Wenn ein nicht autorisierter Benutzer versucht, Anfrage / Mitglieder / MyPage sie auf die Login-Seite korrekt weitergeleitet werden.

Allerdings ist die ReturnUrl Parameter nicht in die Login-Seite geleitet, so dass, wenn der Benutzer authentifiziert, sie auf die Standardseite genommen werden (nennen wir es / Mitglieder / Home) statt / Mitglieder / MyPage.

Frage

Warum?

In einer anderen App, entwickelte in MVC2, der returnUrl QS-Parameter ist da und funktioniert wie erwartet.

Andere Probleme:

Das Autorize Attribut ignoriert wird, wenn beide Controller und Aktionen zu dekorieren.

Auflösung:

Die Abschnitte der web.config nicht korrekt aktualisiert zwischen .NET 3.5 und .NET 4. Siehe Antworten unten.

War es hilfreich?

Lösung

@Marcind put me on the right track, @Darin Dimitrov's answer very instructive of the process involved.

Diagnosis

It seems that the issue was related to a web.config that I did not update properly when merging an existing Web Forms .NET 3.5 app to a .NET 4.0 app. I can't recall how I went about this.

Anyway, by comparing the web.config of my app with a new MVC 3 web.config, I was able to find the extra bits that should not have been there, left over from 3.5 days.

Resolution:

The issue was resolved by correcting the bits in the <authentication><forms> tag in the web.config, as well as the <membership> tag.

Other Issues Caused by this:

Another issue caused by this was the fact that if I decorated a controller with the Authorize attribute, it was ignored, so the controller tried to process info based on the current user, that obviously was null, so all manner of exceptions were fired.

Andere Tipps

It works for me. I created a new project using the ASP.NET MVC 3 RC2, default template, added a MembersController, decorated it with the [Authorize] attribute, run the application, requested /members/index, was redirected to /Account/LogOn?ReturnUrl=%2fmembers%2findex, logged in, was redirected to /members/index. There must be something else wrong with your code.

Here's how it works:

  • The [Authorize] attribute checks if the user is authenticated and if it is not it returns 401 status code.
  • The FormsAuthenticationModule which is part of ASP.NET and handles forms authentication intercepts the 401 status code and redirects to the login page by appending the ReturnUrl parameter to the request which points to the initial request.

The FormsAuthenticationModule module is not specific to ASP.NET MVC, this is standard ASP.NET stuff

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top