c# - 谷歌授权错误错误400 : redirect_uri_mismatch

标签 c# amazon-elastic-beanstalk asp.net-core-webapi google-signin webauthenticator

我的 asp.net core web api 有一个严重的问题,google 登录在本地主机上工作正常,但是当在 ElasticBeanstalk 中的远程服务器上发布时,它会抛出此错误。 所有身份验证过程在本地主机上都正常。请有人帮忙解决这个问题。

这是我正在使用的完全相同的代码。

`公共(public)类 AuthController : ControllerBase { const string callbackScheme = "xamariinessentials";

    [HttpGet("{scheme}")]
    public async Task Get([FromRoute]string scheme)
    {
        var auth = await Request.HttpContext.AuthenticateAsync(scheme);

        if (!auth.Succeeded
            || auth?.Principal == null
            || !auth.Principal.Identities.Any(id => id.IsAuthenticated)
            || string.IsNullOrEmpty(auth.Properties.GetTokenValue("access_token")))
        {
            // Not authenticated, challenge
            await Request.HttpContext.ChallengeAsync(scheme);
        }
        else
        {
            var claims = auth.Principal.Identities.FirstOrDefault()?.Claims;
            var email = string.Empty;
            email = claims?.FirstOrDefault(c => c.Type == System.Security.Claims.ClaimTypes.Email)?.Value;

            // Get parameters to send back to the callback
            var qs = new Dictionary<string, string>
            {
                { "access_token", auth.Properties.GetTokenValue("access_token") },
                { "refresh_token", auth.Properties.GetTokenValue("refresh_token") ?? string.Empty },
                { "expires", (auth.Properties.ExpiresUtc?.ToUnixTimeSeconds() ?? -1).ToString() },
                { "email", email }
            };

            // Build the result url
            var url = callbackScheme + "://#" + string.Join(
                "&",
                qs.Where(kvp => !string.IsNullOrEmpty(kvp.Value) && kvp.Value != "-1")
                .Select(kvp => $"{WebUtility.UrlEncode(kvp.Key)}={WebUtility.UrlEncode(kvp.Value)}"));

            // Redirect to final url
            Request.HttpContext.Response.Redirect(url);
        }
    }`

enter image description here enter image description here

这是 .net 6 中的startup.cs 或program.cs 中的Google 身份验证配置。 enter image description here

这是我的 Google 开发者控制台 enter image description here

最佳答案

您需要接受 XForwardedProto

在 Startup.cs 或 Program.cs 中(在 .net 6 中)

public void ConfigureServices(IServiceCollection services)
{
  ...
    services.Configure<ForwardedHeadersOptions>(options =>
    {
        options.ForwardedHeaders = ForwardedHeaders.XForwardedProto;
    });
   ...  
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
 ...
 app.UseForwardedHeaders();
 ...
}

关于c# - 谷歌授权错误错误400 : redirect_uri_mismatch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70795504/

相关文章:

c# - 随机字符串 c#

php - 在 AWS 上的 symfony 2.8 应用程序中更改根目录 - Elastic Beanstalk

amazon-web-services - Elastic Beanstalk 无法创建

asp.net-core - 升级到 aspnetcore 2.0 后 CORS 不工作

c# - Asp Net Core Web 推送通知

.net - 如何在 Nginx 服务器中将 ASP.NET Core Web API dll 文件作为服务运行?

c# - 如果说话者远离麦克风,Google Speech/NAudio 会有很大的延迟

c# - 移动要托管的 XML 文件

c# - Convert.ChangeType 如何从字符串转换为枚举

amazon-web-services - 如何在应用程序中获取 Elastic Beanstalk 应用程序版本?