简体   繁体   English

负载均衡器“令牌端点的无效HTTP请求”

[英]Load Balancer “Invalid HTTP request for token endpoint”

I have an issue with a IdentityServer behind a load balancer, 我在负载均衡器后面有一个IdentityServer问题,
Just to be clear if I'm using direct ips, evertything works as expected. 为了清楚我是否使用直接的ips,evertything按预期工作。

Currently I have an SSL configuration on LB with a valid certificate that redirects to a HTTP docker-engines hosted on 2 Linux machines. 目前我在LB上有一个SSL配置,其中包含一个有效的证书,该证书可重定向到托管在2台Linux机器上的HTTP docker-engines。

I'm using an MVC hybrid flow on my Client: 我在我的客户端上使用MVC混合流程:

app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
{
    AuthenticationScheme = "oidc",
    SignInScheme = "Cookies",
    Authority = settings.Server,
    RequireHttpsMetadata = "false",
    ClientId = "MyMvcClient",
    ClientSecret = "MyMvcSuperPassword",
    ResponseType =  "code id_token",
    GetClaimsFromUserInfoEndpoint = true,
    SaveTokens = true,
    TokenValidationParameters = new TokenValidationParameters
    {
        NameClaimType = JwtClaimTypes.Name,
        RoleClaimType = JwtClaimTypes.Role,
    },
    Events = new OpenIdConnectEvents()
    {
        OnRemoteFailure = context =>
        {
            context.Response.Redirect("/error");
            context.HandleResponse();
            return Task.FromResult(0);
        }
    },
    "Scope": { "openid", "profile", "email", "myclient", "offline_access" }
});

On the server side, I have the following setup: 在服务器端,我有以下设置:

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<IdentityDbContext>(builder);
    services.AddIdentity<ApplicationUser, ApplicationRole>()
        .AddEntityFrameworkStores<IdentityDbContext, Guid>()
        .AddUserStore<UserStore<ApplicationUser, ApplicationRole, IdentityDbContext, Guid>>()
        .AddRoleStore<RoleStore<ApplicationRole, IdentityDbContext, Guid>>()
        .AddUserManager<LdapUserManager>()
        .AddSignInManager<LdapSignInManager>()
        .AddDefaultTokenProviders()
        .AddIdentityServerUserClaimsPrincipalFactory();
    ...
    string redisSettings = $"{redisHost}:{redisPort},ssl={redisSsl},abortConnect={redisAbortConnect},password={redisPassword}";
    services.AddDataProtection().PersistKeysToRedis(redis, redisKey);
    ...
    // Adds IdentityServer
    services.AddIdentityServer()
        .AddSigningCredential(new X509Certificate2("IdentityServerAuth.pfx"))
        .AddConfigurationStore(builder)
        .AddOperationalStore(builder)
        .AddAspNetIdentity<ApplicationUser>()
        .AddProfileService<IdentityProfileService>();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IServiceProvider serviceProvider)
{
    app.UseIdentity();
    app.UseForwardedHeaders(new ForwardedHeadersOptions
    {
        ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
    });
    // Adds IdentityServer
    app.UseIdentityServer();
}

As I already mentioned, it works for configuration without Load balancer, but client fails with error "Message contains error: 'invalid_request', error_description: 'error_description is null', error_uri: 'error_uri is null'." 正如我已经提到的,它适用于没有Load Balancer的配置,但客户端失败并显示错误“Message contains error:'invalid_request',error_description:'error_description为null',error_uri:'error_uri为null'。”

And on the server side I'm receiving info: IdentityServer4.Hosting.IdentityServerMiddleware[0] Invoking IdentityServer endpoint: IdentityServer4.Endpoints.TokenEndpoint for /connect/token warn: IdentityServer4.Endpoints.TokenEndpoint[0] Invalid HTTP request for token endpoint 在服务器端,我收到信息:IdentityServer4.Hosting.IdentityServerMiddleware [0]调用IdentityServer端点:IdentityServer4.Endpoints.TokenEndpoint for / connect / token warn:IdentityServer4.Endpoints.TokenEndpoint [0]无效的令牌端点的HTTP请求

I have solved the problem. 我已经解决了这个问题。 The solution was found tracing the Request.HttpContext.Connection.RemoteIpAddress It was invalid, and was equal to IP of the Load balancer. 找到的解决方案是跟踪Request.HttpContext.Connection.RemoteIpAddress它是无效的,并且等于负载均衡器的IP。 So the solution was to add the Ip using: 所以解决方案是使用以下方法添加Ip:

options.KnownProxies.Clear();
options.KnownProxies.Add(IPAddress.Parse("LoadBalancer IP"));
app.UseForwardedHeaders(options);

or by mask eg 192.168.1.0/8: 或通过掩码,例如192.168.1.0/8:

options.KnownNetworks.Clear();
options.KnownNetworks.Add(new IPNetwork(IPAddress.Parse("192.168.1.0"), 8));
app.UseForwardedHeaders(options);

In this case the IP of Client will be passed to Identity server and you won't receive exception: 在这种情况下,客户端的IP将传递给身份服务器,您将不会收到异常:

Invalid HTTP request for token endpoint

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将HTTP重定向到负载均衡器后面的HTTPS - Redirecting HTTP to HTTPS behind load balancer 在我的Web API端点中解析HTTP POST请求正文时出错 - Error parsing HTTP POST request body in my web api endpoint 部署在负载均衡器后面时,无法解密Anti-Fogery令牌 - Anti-Fogery token cannot be decrypted when deployed behind a load balancer 身份 server4 在部署到生产时收到连接/令牌端点的“invalid_grant”错误 - identity server4 getting "invalid_grant" error for the connect/token endpoint when deployed to production 令牌端点上的自定义令牌响应 - Custom Token Response on Token Endpoint 在API ResponseCache NoStore = true时是否缓存HTTP请求? 在特定的API端点上 - Does an HTTP request get cached when in the API ResponseCache NoStore = true? On a particular API endpoint ASP.Net Core 6,SPA,端点 - 我无法从 http 调用 controller 获取请求 - ASP.Net Core 6, SPA, endpoint - I can't call a controller from http get request 如果用户发布 http 请求后令牌过期,则刷新令牌概念如何工作 - how refresh token concept works if the token expires once user post http request Map / Route HTTP 根据查询参数值向端点请求? asp.net 核心 3.x - Map / Route HTTP Request to endpoint based on query parameter value? asp.net core 3.x Razor 页面 - web 站点空闲时出现 HTTP 错误 400(无效的防伪令牌) - Razor Pages - HTTP Error 400 (Invalid Anti Forgery Token) when idle the web site
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM