2016-10-28 4 views
0

Ich habe einen Link zu/ForgotPassword auf meiner Anmeldeseite. Wenn ich darauf klicke, werde ich einfach zu/Login? ReturnUrl =% 2fForgotPassword weitergeleitet. Dies ist eine MVC5-Anwendung mit formularbasierter Authentifizierung. Ich versuche, AllowAnonymous zu verwenden, aber es funktioniert nicht. Web Config:Ermöglichen Sie anonymen Zugriff auf ForgotPassword Seite

<location path="."> 
    <system.web> 
    <authorization> 
    <deny users="?"/> 
    </authorization> 
    </system.web> 
    </location> 
    <location path="Content"> 
    <system.web> 
    <authorization> 
    <allow users="*"/> 
    </authorization> 
    </system.web> 
    </location> 
    <location path="Scripts"> 
    <system.web> 
    <authorization> 
    <allow users="*"/> 
    </authorization> 
    </system.web> 
    </location> 
    <location path="App_Themes"> 
    <system.web> 
    <authorization> 
    <allow users="*"/> 
    </authorization> 
    </system.web> 
</location> 

<authentication mode="Forms"> 
    <forms loginUrl="~/Login" timeout="15"/> 
</authentication> 

ForgotPassword Controller:

[AllowAnonymous] 
public class ForgotPasswordController : Controller 
{ 
    [AllowAnonymous] 
    public ActionResult Index() 
    {   
     return View(); 
    } 
} 

Schön wäre es. Abgesehen von einem _ViewStart kann ich nichts finden, was verwendet wird, das mit AllowAnonymous dekoriert werden sollte.

Was fehlt mir hier? Vielen Dank!

Antwort

1

Verwenden Sie keine Autorisierungsvariablen in we.config, verwenden Sie nur Autorisierungsattribute in Controllern und Aktionen.

Autorisierungstags in web.config sind nützlich, um den Zugriff auf Dateien zu erlauben/einzuschränken und es funktioniert gut mit Webformularen und statischen Inhalten.

Aber in MVC gibt es Routen, die zu Controllern und Aktionen zugeordnet ist, und Sie können verschiedene Aktionen in der gleichen Controller mit verschiedenen Autorisierungs-Tags haben, zB: Sie können [Autorisieren] für den gesamten Controller verwenden und nur eine zulassen Aktion für anonyme Benutzer, indem das [AllowAnonymous] -Attribut nur für die gewünschte Aktion verwendet wird.

Lesen Sie hier mehr: https://weblogs.asp.net/jongalloway/asp-net-mvc-authentication-global-authentication-and-allow-anonymous

Verwandte Themen