3

Ich habe folgendes Setup in meinem MVC 6 ASP.NET 5 Projekt:Ändern CookieAuthenticationOptions LoginPath OnRedirectToReturnUrl Ereignis

Startup.cs im Configure-Methode:

app.UseCookieAuthentication(options => 
{ 
    options.AuthenticationScheme = "Cookie"; 
    options.LoginPath = new PathString("/<TENANT>/account/signin/"); 
    options.AccessDeniedPath = new PathString("/<TENANT>/account/unauthorised/"); 
    options.AutomaticAuthenticate = true; 
    options.AutomaticChallenge = true; 
    options.Events = new CookieAuthenticationEvents 
    { 
     OnRedirectToReturnUrl = MyClass.RedirectToReturnUrlAsync 
    }; 
}); 

Events Klasse:

public static class MyClass 
{ 
    public static async Task RedirectToReturnUrlAsync(CookieRedirectContext context) 
    { 
     context.Options.LoginPath = new PathString("/<HERE I PLAN TO PUT LOGIC TO FIGURE OUT TENANT FROM CONTEXT>/account/signin"); 
    } 

} 

Sagen wir, ein Benutzer geht zu der folgenden URL:

http://localhost/mycompany/securecontroller/secureaction 

Ich möchte, dass die Cookie-Middleware der Benutzer zu umleiten:

http://localhost/mycompany/account/signin 

Das Problem ist der Code „MyClass.RedirectToReturnUrlAsync“ nie getroffen wird, wenn ein Redirect RÜCKGABEURL geschieht, so kann ich nicht finden, die Gelegenheit zu ändern der LoginPath zur Laufzeit.

Ich vermute, ich habe etwas falsch in meinem Setup. Ist jemand jemals auf dieses Problem gestoßen?

Hooroo

Antwort

5

Ok, ich denke ich habe es herausgefunden. Ich wurde auf das Problem von der falschen Winkel suchen (und nach einem etwas Schlaf bekommen!)

app.UseCookieAuthentication(options => 
{ 
    options.AuthenticationScheme = "Cookie"; 
    options.LoginPath = new PathString("/<TENANT>/account/signin/"); 
    options.AccessDeniedPath = new PathString("/<TENANT>/account/unauthorised/"); 
    options.AutomaticAuthenticate = true; 
    options.AutomaticChallenge = true; 
    options.Events = new MyCookieAuthenticationEvents(); 
}); 

der richtige Weg, Ihre eigenen Events Cookie-Authentifizierung zu erstellen wäre, ergeben sich aus der CookieAuthenticationEvents Objekt und überschreiben Sie die Ereignisse, die Sie ‚d wie die Gewohnheit, so etwas wie diese:

public class MyCookieAuthenticationEvents : CookieAuthenticationEvents 
{ 
    public override Task RedirectToLogin(CookieRedirectContext context) 
    { 
     context.RedirectUri = <PUT LOGIC HERE TO REPLACE YOUR REDIRECT URI> 
     return base.RedirectToLogin(context); 
    } 
} 

ich war auch das falsche Ereignis in meinem vorherigen Versuch Targeting. In meinem Fall war die Methode "RedirectToLogin" die richtige Methode zum Überschreiben.

Hooroo

Verwandte Themen