奥尔姆,任怡旭,肤色暗沉吃什么
dotnet core 跨平台是微软伟大的创举,脱离iis后服务器成本都降低了。
这不,采用abp搞了个小项目,部署到centos后发现审计日志里面的ip信息不对。
这个问题在.net 4.5下处理过,记得当时是继承 webclientinfoprovider重写getclientipaddress。
将代码拿来后发现dotnet core下报错。
跟进后发现 dotnet core下使用的是 abp.aspnetcore.mvc.auditing下的:httpcontextclientinfoprovider
修改代码如下,将其放在 xxx.web.core 的extensions目录:
public class webclientinfoproviderfix : iclientinfoprovider { public string browserinfo => getbrowserinfo(); public string clientipaddress => getclientipaddress(); public string computername => getcomputername(); public ilogger logger { get; set; } private readonly ihttpcontextaccessor _httpcontextaccessor; private readonly httpcontext _httpcontext; /// <summary> /// creates a new <see cref="httpcontextclientinfoprovider"/>. /// </summary> public webclientinfoproviderfix(ihttpcontextaccessor httpcontextaccessor) { _httpcontextaccessor = httpcontextaccessor; _httpcontext = httpcontextaccessor.httpcontext; logger = nulllogger.instance; } protected virtual string getbrowserinfo() { var httpcontext = _httpcontextaccessor.httpcontext ?? _httpcontext; return httpcontext?.request?.headers?["user-agent"]; } protected virtual string getclientipaddress() { try { var httpcontext = _httpcontextaccessor.httpcontext ?? _httpcontext; var headers = httpcontext?.request.headers; if (headers!=null&&headers.containskey("x-forwarded-for")) { httpcontext.connection.remoteipaddress = ipaddress.parse(headers["x-forwarded-for"].tostring().split(',', stringsplitoptions.removeemptyentries)[0]); } return httpcontext?.connection?.remoteipaddress?.tostring(); } catch (exception ex) { logger.warn(ex.tostring()); } return null; } protected virtual string getcomputername() { return null; //todo: implement! } }
然后xxxwebcoremodule.cs中添加如下:
//jieky@2019-1-24 针对 获取客户端ip异常的处理 configuration.replaceservice(typeof(abp.auditing.iclientinfoprovider), () => { iocmanager.register<abp.auditing.iclientinfoprovider, extensions.webclientinfoproviderfix>(abp.dependency.dependencylifestyle.transient); });
nginx配置例子
server { listen 5002; access_log off; location / { proxy_set_header x-real-ip $remote_addr; proxy_set_header host $host; proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; proxy_pass http://localhost:5000; } }
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Blazor server side 自家的一些开源的, 实用型项目的进度之 CEF客户端
.NET IoC模式依赖反转(DIP)、控制反转(Ioc)、依赖注入(DI)
vue+.netcore可支持业务代码扩展的开发框架 VOL.Vue 2.0版本发布
网友评论