redirect_uri中的斜线号

问题描述 投票:0回答: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 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中没有多余的斜线。

我尝试从返回网址中修剪斜杠:

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);
}

但是此解决方案无济于事,它看起来也不是一个好的解决方案。

asp.net-core redirect uri google-authentication base-path
1个回答
0
投票

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

app.Use((context, next) =>
{
    context.Request.PathBase = "/anystring/".TrimEnd('/');
    return next.Invoke();
});
© www.soinside.com 2019 - 2024. All rights reserved.