【问题标题】:Google authentication with load balancer in c#在 C# 中使用负载均衡器进行 Google 身份验证
【发布时间】:2021-05-12 17:32:49
【问题描述】:

我以这种方式使用 Traefik 实现了负载均衡器:

http:
  routers:
    mydomain-web-http:
      service: mydomain-web
      rule: "Host(`mydomain.com`,`www.mydomain.com`)"
      entryPoints:
        - http
      middlewares:
        - https-redirect
    mydomain-web-https:
      service: mydomain-web
      rule: "Host(`mydomain.com`,`www.mydomain.com`)"
      entryPoints:
        - https
      tls:
        certResolver: letsEncrypt

  services:
    mydomain-web:
      loadBalancer:
        sticky:
          cookie:
            name: mydomain_sticky
            secure: true
            httpOnly: true
        passHostHeader: false
        servers:
          - url: https://www2.mydomain.com
          - url: https://www1.mydomain.com

现在我配置了一个 webapp 客户端 OAuth 2.0。用这种方式用谷歌登录:

public void ConfigureAuth(IAppBuilder app)
    {
        var googleOAuth2AuthenticationOptions = new GoogleOAuth2AuthenticationOptions
                    {
                        ClientId = "xxx.apps.googleusercontent.com",
                        ClientSecret = "-sn_xxx",
                     
                        Provider = new GoogleOAuth2AuthenticationProvider()
                        {
                            OnAuthenticated = async context =>
                            {
                                context.Identity.AddClaim(new Claim("GoogleAccessToken", context.AccessToken));
                            }
                        }
                    };
        
                    app.UseGoogleAuthentication(googleOAuth2AuthenticationOptions);
        
        
    }

在使用负载均衡器进行配置之前,此代码工作正常,但现在我收到此错误:

错误 400:redirect_uri_mismatch 请求中的重定向 URI https://www2.mydomain.com/signin-google 与授权给 OAuth 客户端的不匹配...

我需要为我的“主”域 (www.mydomain.com) 授权 google 登录...我尝试以这种方式实现此目的:

        app.Use((ctx, next) =>
        {
            ctx.Request.Host = new HostString("www.mydomain.com");
            return next();
        });

没有运气......

【问题讨论】:

    标签: c# oauth-2.0 traefik


    【解决方案1】:

    在经历了很多痛苦之后,我通过以这种方式设置架构来解决:

       app.Use((ctx, next) =>
            {
                ctx.Request.Scheme = "https";
                ctx.Request.Host = new HostString("www.mydomain.com");
                return next();
            });
    

    【讨论】:

      猜你喜欢
      • 2018-07-06
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 2020-11-05
      • 2020-08-31
      • 2011-03-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多