获取WCF的客户端IP地址,操作后
我试图通过此链接确定客户端的 IP 地址: http://www.danrigsby.com/blog/index.php/2008/05/21/get-the-clients-address-in-wcf/
在 .Net 3.0 中,没有可靠的方法来获取 连接到 WCF 服务的客户端。在 .Net 3.5 中,一个新属性是 引入了名为RemoteEndpointMessageProperty。该属性给出 客户端连接进入的 IP 地址和端口 服务开启。获取这些信息非常简单。 只需从当前的 IncomingMessageProperties 中提取它即可 OperationContext通过RemoteEndpointMessageProperty.Name而获取 地址和端口属性。
> [ServiceContract] public interface IMyService {
> [OperationContract]
> string GetAddressAsString(); }
>
> public class MyService : IMyService {
> public string GetAddressAsString()
> {
> RemoteEndpointMessageProperty clientEndpoint =
> OperationContext.Current.IncomingMessageProperties[
> RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
>
> return String.Format(
> "{0}:{1}",
> clientEndpoint.Address, clientEndpoint.Port);
> } }
注意事项:
- 此属性仅适用于 http 和 tcp 传输。在所有其他方面 传输,例如 MSMQ 和 NamedPipes,该属性不会 可用的。
- 地址和端口由套接字或http.sys报告 服务机器的。因此,如果客户端通过 VPN 或其他方式进入 修改该地址的其他代理,该新地址将是 代表而不是客户端的本地地址。这是可取的 很重要,因为这是服务的地址和端口 看到客户的样子,而不是客户看到自己的样子。这也意味着 可能存在欺骗行为。客户或其他什么东西 客户端和服务器之间的通信可能会欺骗地址。所以,不要使用 任何安全决策的地址或端口,除非您添加一些 其他自定义检查机制。
- 如果您正在使用双面打印 服务,那么该服务不仅会填充此属性 对于客户端,但客户端也会填充此属性 对于来自该服务的每次调用的服务。
我有WebInvoke/Post和WebGet的操作合同。当客户端请求是 WebGet 时,该代码有效。但是当客户端请求是WebInvoke时,我将获取WCF主机IP。有什么解决办法吗?谢谢。
这是界面
[OperationContract]
[WebGet(UriTemplate = RestTemplate.hello_get)]
Stream hello_get();
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = RestTemplate.hello_post)]
Stream hello_post();
// Code for getting IP
private string getClientIP()
{
//WebOperationContext webContext = WebOperationContext.Current;
OperationContext context = OperationContext.Current;
MessageProperties messageProperties = context.IncomingMessageProperties;
RemoteEndpointMessageProperty endpointProperty =
messageProperties[RemoteEndpointMessageProperty.Name]
as RemoteEndpointMessageProperty;
return endpointProperty.Address;
}
public Stream hello_get()
{
string ip = getClientIP();
...
}
public Stream hello_post()
{
string ip = getClientIP();
...
}
I am trying to determine the client's IP address following this link: http://www.danrigsby.com/blog/index.php/2008/05/21/get-the-clients-address-in-wcf/
In .Net 3.0 there was not a reliable way to obtain the address of the
client connecting to a WCF service. In .Net 3.5 a new property was
introduced called RemoteEndpointMessageProperty. This property gives
you the IP address and port that the client connection came into the
service on. Obtaining the this information is pretty straight forward.
Just pull it from the IncomingMessageProperties of the current
OperationContext by the RemoteEndpointMessageProperty.Name and access
the Address and Port properties.
> [ServiceContract] public interface IMyService {
> [OperationContract]
> string GetAddressAsString(); }
>
> public class MyService : IMyService {
> public string GetAddressAsString()
> {
> RemoteEndpointMessageProperty clientEndpoint =
> OperationContext.Current.IncomingMessageProperties[
> RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
>
> return String.Format(
> "{0}:{1}",
> clientEndpoint.Address, clientEndpoint.Port);
> } }
Things to note:
- This property only works on http and tcp transports. On all other
transports such as MSMQ and NamedPipes, this property will not be
available.- The address and port are reported by the socket or http.sys
of the service’s machine. So if the client came in over a VPN or some
other proxy that modified the address, that new address will be
represented instead of the client’s local address. This is desirable
and important because this is the address and port that the service
sees the client as, not as the client sees itself as. This also means
that there could be some spoofing going on. A client or something in
between the client and server could spoof an address. So, do not use
the address or port for any security decisions unless you add in some
other custom checking mechanisms.- If you are using duplexing on your
service, then not only will the service have this property populated
for the client, but the client will also have this property populated
for the service for each call from that service.
I have operationContracts of WebInvoke/Post and WebGet. The code works when the client request is a WebGet. But when the client request is a WebInvoke, I will get the WCF host IP. Any solution? Thanks.
Here is the interface
[OperationContract]
[WebGet(UriTemplate = RestTemplate.hello_get)]
Stream hello_get();
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = RestTemplate.hello_post)]
Stream hello_post();
// Code for getting IP
private string getClientIP()
{
//WebOperationContext webContext = WebOperationContext.Current;
OperationContext context = OperationContext.Current;
MessageProperties messageProperties = context.IncomingMessageProperties;
RemoteEndpointMessageProperty endpointProperty =
messageProperties[RemoteEndpointMessageProperty.Name]
as RemoteEndpointMessageProperty;
return endpointProperty.Address;
}
public Stream hello_get()
{
string ip = getClientIP();
...
}
public Stream hello_post()
{
string ip = getClientIP();
...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您尝试过使用 HttpContext 吗?它并非在所有 WCF 模式中都可用,但这可能取决于您的环境:
Have you tried using the HttpContext? It's not available in all WCF modes but this could work depending on your environment: