首页 > 代码库 > wcf 获取客户端 IP

wcf 获取客户端 IP

http://stackoverflow.com/questions/3937773/wcf-security-using-client-ip-address

 

var context = OperationContext.Current;var props = context.IncomingMessageProperties;var endpoint = props[RemoteEndpointMessageProperty.Name];return ((RemoteEndpointMessageProperty)endpoint).Address;


You can access HTTP headers in the same way. Instead of RemoteEndpointMessageProperty you have to use HttpRequestMessageProperty. This property contains Headers name value collection so you should be able to get any HTTP header from incoming request.


Ultimately, this answer is valid, so I‘m accepting it, but .NET 3.5 and greater added a WebOperationContext class to the System.ServiceModel.Web namespace which makes this a bit easier. 

Yes, but this solution is general for all Http based bindings. WebOperationContext should work only for REST services (exposed on WebHttpBinding). 

wcf 获取客户端 IP