【发布时间】:2020-01-29 21:49:45
【问题描述】:
我使用 ocelot 作为我的微服务的 API 网关,使用 IdentityServer4 进行身份验证。在 ocelot 配置文件中,我添加了“AuthenticationOptions”并设置了 api 密钥。在 Startup 中,我添加了身份服务器。在身份服务器中,我使用标头中的值来动态构建连接字符串。当我发送获取令牌的请求时,可以在身份服务中访问标头。但是当我使用令牌发送下一个请求时,原始标头不可用。在身份服务中只能看到“主机”标头。
有没有办法在将请求路由到身份服务器时保留原始标头?
Startup.cs(添加身份服务器)
services
.AddAuthentication()
.AddIdentityServerAuthentication("APIParts", options =>
{
options.Authority = "http://localhost:60168";
options.RequireHttpsMetadata = false;
options.ApiName = "Parts";
options.SupportedTokens = SupportedTokens.Both;
});
ocelot.json
ReRoutes": [
{
"DownstreamPathTemplate": "/connect/token",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 60168
}
],
"UpstreamPathTemplate": "/token",
"UpstreamHttpMethod": [ "Post" ]
},
{
"DownstreamPathTemplate": "/api/Parts/Inventory",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 65241
}
],
"UpstreamPathTemplate": "/api/Parts/Inventory",
"AuthenticationOptions": {
"AuthenticationProviderKey": "APIParts",
"AllowedScopes": []
}
}]
【问题讨论】:
-
在深入探讨之前,您能否解释一下为什么要使用不同的端口来进行 Identity Server 身份验证和 API?我认为可能存在问题,因为当生成 API 请求时,身份授权尝试在 API 所在的同一端口上验证令牌,所以你可以给两个相同的端口并尝试一下。
-
您能否发布一些代码来显示您如何尝试访问标头以构建连接字符串?此外,您要阅读什么标题?如果是主机标头,您将遇到问题。
标签: c# .net-core identityserver4 ocelot