redirect_uri 中的额外斜杠

Extra slash in redirect_uri

我在 Startup.cs.

中为我的应用程序添加了 google 身份验证
services.AddAuthentication()
    .AddGoogle(googleOptions =>
    {
        googleOptions.ClientId = "xxx";
        googleOptions.ClientSecret = "xxx";
    });

我还有一个重定向到外部提供商的页面。

public IActionResult OnPost(string provider, string returnUrl = null)
{
    string redirectUrl = Url.Page("./ExternalLogin", pageHandler: "Callback", values: new { returnUrl });
    Microsoft.AspNetCore.Authentication.AuthenticationProperties properties = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl);
    return new ChallengeResult(provider, properties);
}

在我的应用程序中,我需要指定 基本路径

<base href="/anystring/" />
app.Use((context, next) =>
{
    context.Request.PathBase = "/anystring/";
    return next.Invoke();
});

问题是 redirect_uri 在重定向到外部提供商时不正确 - 组合路径中有一个额外的斜杠: http://localhost:4200/anystring//signin-google.

预期 redirect_uri 是: http://localhost:4200/anystring/signin-google

当我尝试使用任何其他外部登录提供商时出现同样的问题。

但是当 '/' 用作基本路径时 - 一切正常并且 redirect_uri.

中没有额外的斜杠

我试图 trim 来自 return url 的尾部斜线:

public IActionResult OnPost(string provider, string returnUrl = null)
{
    returnUrl = returnUrl?.TrimEnd('/'); // trim trailing slash
    string redirectUrl = Url.Page("./ExternalLogin", pageHandler: "Callback", values: new { returnUrl });
    Microsoft.AspNetCore.Authentication.AuthenticationProperties properties = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl);
    return new ChallengeResult(provider, properties);
}

但是这个解决方案没有帮助,看起来也不是一个好的解决方案。

应修整路径基础以解决此问题。

app.Use((context, next) =>
{
    context.Request.PathBase = "/anystring/".TrimEnd('/');
    return next.Invoke();
});