asp.net core使用jexus部署在linux无法正确 获取远程ip的解决办法
2017-08-11 09:32
1141 查看
asp.net core程序部署在centos7(下面的解决方案,其他系统都能使用,这里只是我自己部署在centos7),使用服务器jexus进行部署,AppHost模式。
因为请求是由jexus进行了转发的,所以asp.net zero获取的ip永远都是127.0.0.1.。
解决方案:
使用由Jexus作者宇内流云提供的JwsIntegration替换IISIntegration,它改变默认从请求头获取ip的规则,改为由 “X-Original-For”获取远程ip(经测试 使用"X-Real-IP"也能获取)。
JwsIntegration.cs:
使用方法:
因为请求是由jexus进行了转发的,所以asp.net zero获取的ip永远都是127.0.0.1.。
解决方案:
使用由Jexus作者宇内流云提供的JwsIntegration替换IISIntegration,它改变默认从请求头获取ip的规则,改为由 “X-Original-For”获取远程ip(经测试 使用"X-Real-IP"也能获取)。
JwsIntegration.cs:
/// <summary> /// 用于处理客户IP地址、端口的HostBuilder中间件 /// </summary> public static class WebHostBuilderJexusExtensions { /// <summary> /// 启用JexusIntegration中间件 /// </summary> /// <param name="hostBuilder"></param> /// <returns></returns> public static IWebHostBuilder UseJexusIntegration(this IWebHostBuilder hostBuilder) { if (hostBuilder == null) { throw new ArgumentNullException(nameof(hostBuilder)); } // 检查是否已经加载过了 if (hostBuilder.GetSetting(nameof(UseJexusIntegration)) != null) { return hostBuilder; } // 设置已加载标记,防止重复加载 hostBuilder.UseSetting(nameof(UseJexusIntegration), true.ToString()); // 添加configure处理 hostBuilder.ConfigureServices(services => { services.AddSingleton<IStartupFilter>(new JwsSetupFilter()); }); return hostBuilder; } } class JwsSetupFilter : IStartupFilter { public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next) { return app => { app.UseMiddleware<JexusMiddleware>(); next(app); }; } } class JexusMiddleware { readonly RequestDelegate _next; public JexusMiddleware(RequestDelegate next, ILoggerFactory loggerFactory, IOptions<IISOptions> options) { _next = next; } public async Task Invoke(HttpContext httpContext) { var headers = httpContext.Request.Headers; try { if (headers != null && headers.ContainsKey("X-Original-For")) { var ipaddAdndPort = headers["X-Original-For"].ToArray()[0]; var dot = ipaddAdndPort.IndexOf(":", StringComparison.Ordinal); var ip = ipaddAdndPort; var port = 0; if (dot > 0) { ip = ipaddAdndPort.Substring(0, dot); port = int.Parse(ipaddAdndPort.Substring(dot + 1)); } httpContext.Connection.RemoteIpAddress = System.Net.IPAddress.Parse(ip); if (port != 0) httpContext.Connection.RemotePort = port; } } finally { await _next(httpContext); } } }
使用方法:
相关文章推荐
- [C#]使用 C# 代码实现拓扑排序 dotNet Core WEB程序使用 Nginx反向代理 C#里面获得应用程序的当前路径 关于Nginx设置端口号,在Asp.net 获取不到的,解决办法 .Net程序员 初学Ubuntu ,配置Nignix 夜深了,写了个JQuery的省市区三级级联效果
- ASP.NET在IIS上部署使用Oracle数据库无法连接数据库解决方法
- ASP.NET在IIS上部署使用Oracle数据库无法连接数据库解决方法
- ASP.NET在IIS上部署使用Oracle数据库无法连接数据库解决方法
- asp.net使用response.redirect后无法弹出对话框解决办法
- IIS部署asp.net报404错误 iis与Framework安装顺序造成无法访问网站的解决办法
- 把Asp.Net Core 2.0部署在Linux上,使用Nginx代理服务器,并且用Systemctl命令以服务的方式监听项目
- js_html_input中autocomplete="off"在chrom中失效的解决办法 使用JS模拟锚点跳转 js如何获取url参数 C#模拟httpwebrequest请求_向服务器模拟cookie发送 实习期学到的技术(一) LinqPad的变量比较功能 ASP.NET EF 使用LinqPad 快速学习Linq
- [未能将网站 xxxxxxx配置为使用 ASP.NET 4.0。为了使此网站正确运行,您必须将它手动配置为使用 ASP.NET 4.0] 问题的解决办法
- IE7,IE8,上传文件控件input file和asp.net FileUpload控件无法获取完整路径的解决办法
- ASP.NET在IIS上部署使用Oracle数据库无法连接数据库解决方法(转载)
- VisualStudio2017下ASP.NET CORE的TagHelper智能提示不能使用的解决办法
- Asp.Net使用代理IP远程获取数据
- ASP.NET在IIS上部署使用Oracle数据库无法连接数据库解决方法
- VisualStudio2017下ASP.NET CORE的TagHelper智能提示不能使用的解决办法
- LINUX 下Jexus部署ASP.NET Core WebApi
- asp.net中的服务器端控件 textbox 设为只读属性后无法获取JS给其赋的值解决办法