1

我在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 Path

<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 中没有额外的斜杠。

我试图从返回 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);
}

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

4

1 回答 1

1

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

app.Use((context, next) =>
{
    context.Request.PathBase = "/anystring/".TrimEnd('/');
    return next.Invoke();
});
于 2019-12-05T10:18:13.577 回答