c# - 如何在 .core 中更改 Swagger 的基本 url 取决于请求
问题描述
在旧版本的 Swagger(.Net 框架)中,我使用了这个函数来更改 URL:
RootUrl(req => ComputeHostAsSeenByOriginalClient(req))
实际上我从请求中计算了基本 URL(我的应用程序位于负载均衡器后面)
新的 .core 方法是使用这个RoutePrefix
问题:
RoutePrefix是Property而不是Action,所以我没有HttpRequestMessage
这是ComputeHostAsSeenByOriginalClient
完整的代码:
public static string ComputeHostAsSeenByOriginalClient(HttpRequestMessage req)
{
var authority = req.RequestUri.Authority;
var scheme = req.RequestUri.Scheme;
if (req.Headers.Contains("X-Forwarded-Host"))
{
//we are behind a reverse proxy, use the host that was used by the client
var xForwardedHost = req.Headers.GetValues("X-Forwarded-Host").First();
//when multiple apache httpd are chained, each proxy append to the header
//with a comma (see //https://httpd.apache.org/docs/2.4/mod/mod_proxy.html#x-headers).
//so we need to take only the first host because it is the host that was
//requested by the original client.
//note that other reverse proxies may behave differently but
//we are not taking care of them...
var firstForwardedHost = xForwardedHost.Split(',')[0];
authority = firstForwardedHost;
}
if (req.Headers.Contains("X-Forwarded-Proto"))
{
//now that we have the host, we also need to determine the protocol used by the
//original client.
//if present, we are using the de facto standard header X-Forwarded-Proto
//otherwise, we fallback to http
//note that this is extremely brittle, either because the first proxy
//can "forget" to set the header or because another proxy can rewrite it...
var xForwardedProto = req.Headers.GetValues("X-Forwarded-Proto").First();
if (xForwardedProto.IndexOf(",") != -1)
{
//when multiple apache, X-Forwarded-Proto is also multiple ...
xForwardedProto = xForwardedProto.Split(',')[0];
}
scheme = xForwardedProto;
}
//no reverse proxy mean we can directly use the RequestUri
return scheme + "://" + authority;
}
知道如何解决这个问题吗?
解决方案
尝试使用this.Request
. 您可以从 访问请求对象Controller
。
public string ComputeHostAsSeenByOriginalClient()
{
var req = this.Request;
var scheme = req.Scheme;
var authority = this.Request.Host.Value.ToString();
if (req.Headers.ContainsKey("X-Forwarded-Host"))
{
//we are behind a reverse proxy, use the host that was used by the client
StringValues xForwardedHost = "";
if (req.Headers.TryGetValue("X-Forwarded-Host", out xForwardedHost))
{
//when multiple apache httpd are chained, each proxy append to the header
//with a comma (see //https://httpd.apache.org/docs/2.4/mod/mod_proxy.html#x-headers).
//so we need to take only the first host because it is the host that was
//requested by the original client.
//note that other reverse proxies may behave differently but
//we are not taking care of them...
var firstForwardedHost = xForwardedHost.First().ToString().Split(',')[0];
authority = firstForwardedHost;
}
}
if (req.Headers.ContainsKey("X-Forwarded-Proto"))
{
//now that we have the host, we also need to determine the protocol used by the
//original client.
//if present, we are using the de facto standard header X-Forwarded-Proto
//otherwise, we fallback to http
//note that this is extremely brittle, either because the first proxy
//can "forget" to set the header or because another proxy can rewrite it...
StringValues xForwardedProto = "";
if (req.Headers.TryGetValue("X-Forwarded-Proto", out xForwardedProto))
{
if (xForwardedProto.First().ToString().IndexOf(",") != -1)
{
//when multiple apache, X-Forwarded-Proto is also multiple ...
xForwardedProto = xForwardedProto.First().ToString().Split(',')[0];
}
scheme = xForwardedProto;
}
}
//no reverse proxy mean we can directly use the RequestUri
return scheme + "://" + authority;
}
推荐阅读
- elasticsearch - 如何使用 fluentd 从 Kubernetes 日志中提取严重性、时间、消息等字段
- react-native - 刷新 webview 反应原生
- javascript - 如何从快速后端服务器获取和显示图像到 React js 前端?
- python - 如何根据多列拆分csv
- reactjs - 在 TypeScript 中使用任意数量的子组件反应组件
- eclipse - Eclipse 插件 junit-tools 使用 @MethodRef 生成代码,但未定义
- laravel - 如何以 Laravel Eloquent 方式获取数据
- python - 有没有办法在 Python 中找到二维数组中整行数字的索引?
- javascript - 如何在 PayPal 结帐集成中将变量从 js 传递到 php?
- css - 悬停功能和渲染无法按预期工作以覆盖图像