In der vorherigen ASP.NET-MVC gab es eine Option zum Umleiten zur Anmeldeaktion, wenn der Benutzer nicht authentifiziert wurde.
Ich brauche das gleiche mit ASP.NET Core, also habe ich:
[Authorize]
zu einer willkürlichen HandlungIch erwarte keine Umleitung, da ich sie nicht konfiguriert habe. ABER es wird automatisch zur Login Aktion weitergeleitet!
Wo/wie ist diese Option eingestellt?
Sie können den Pfad mit der Klasse CookieAuthenticationOptions
konfigurieren.
Etwas wie das.
app.UseCookieAuthentication(new CookieAuthenticationOptions {
LoginPath = new PathString("/Login/"),
AuthenticationType = "My-Magical-Authentication",
// etc...
},
});
Mit der aktuellen Aspnet Core Version (2.1.0) hat sich dies geändert, jetzt können die Erweiterungen verwendet werden:
services.ConfigureApplicationCookie(options => options.LoginPath = "/login");
oder
services
.AddAuthentication()
.AddCookie(options =>
{
options.LoginPath = "/login";
options.LogoutPath = "/logout";
});
Sie können mehr über die Migration auf 2.0 erfahren in diesem Artikel .
Für alle Interessierten ist dies auch über den AddIdentity-Dienstanbieter möglich.
services.AddIdentity<User, IdentityRole>(options =>
{
options.Cookies.ApplicationCookie.AutomaticAuthenticate = true;
options.Cookies.ApplicationCookie.AutomaticChallenge = true;
options.Cookies.ApplicationCookie.LoginPath = "/Auth/Login";
})
.AddEntityFrameworkStores<MehandiContext>()
.AddDefaultTokenProviders();
Und wie hier erklärt: https://stackoverflow.com/a/41643105/5784635
Ich habe es im April 2017 versucht und "Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.1.0"
leitet nicht weiter Ich musste den 1.0.1
Ausführung
Die Weiterleitung funktionierte in meiner App überhaupt nicht und keine der hier aufgeführten Lösungen hat sie behoben, aber mit Status Code Pages
hat:
app.UseStatusCodePages(async context =>
{
var response = context.HttpContext.Response;
if (response.StatusCode == (int)HttpStatusCode.Unauthorized ||
response.StatusCode == (int)HttpStatusCode.Forbidden)
response.Redirect("/Authentication");
});