简体   繁体   中英

How to force absolute uri kind for returnUrl in ASP.NET Core login redirects?

I use this config for my auth operations

.AddCookie(SchemeName, c =>
{
    c.LoginPath = "/login";
    c.LogoutPath = "/login/logout";
    c.Events.OnRedirectToAccessDenied = context =>
    {
        context.Response.StatusCode = 403;
        return Task.CompletedTask;
    };
    c.ExpireTimeSpan = CookieLifetime;
})

It works fine and redirects to my LoginController Index method when needed and passes returnUrl parameter of page caused redirect as well, but this returnUrl parameter is relative. How can I force ASP.NET Core to pass absolute url instead?

I managed to do it with this code, but I am not sure that this is a good approach

c.Events.OnRedirectToLogin = context =>
{
    var uri = new Uri(context.RedirectUri);
    var ru = $"{uri.GetLeftPart(UriPartial.Path)}?returnUrl={context.Request.GetEncodedUrl()}";
    context.Response.Redirect(ru); 
    return Task.CompletedTask;
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM