How to get the IP address of a WCF remote endpoint?

╄→尐↘猪︶ㄣ 提交于 2019-12-08 14:28:31

问题


Is there a way to get the remote IP Address of a WCF connection?

I guess the reason why it's not built-in into the WCF framework is that WCF can work with non TCP/IP bindings, so the IP Address is not always meaningful.

However, the information would make sense for all the widely used bindings (As far as I know : BasicHttp, DualHttp, WSHttp and NetTcp).

The IP address is probably accessible using reflection, but I'd rather find a documented way to get it rather than hacking into the framework classes.

I've googled on the issue, and it seems a lot of people have run into it without finding a decent solution (The usual answer is to rely on the message headers, but this implies trusting the client to provide its real IP Address, which is not an option if you want to log the IP Address for security reasons)


回答1:


Apparently it has been added in 3.5 via RemoteEndpointMessageProperty; see here.




回答2:


OperationContext context = OperationContext.Current;
MessageProperties properties = context.IncomingMessageProperties;
RemoteEndpointMessageProperty endpoint = properties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
string address = endpoint.Address;



回答3:


if you're on the service side and want to get the client IP, you have to check the OperationContext's Message Properties. look here or here for example



来源:https://stackoverflow.com/questions/391142/how-to-get-the-ip-address-of-a-wcf-remote-endpoint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!