【问题标题】:Get client IP address for WCF, post operation获取 WCF 的客户端 IP 地址,发布操作
【发布时间】:2011-04-04 13:48:37
【问题描述】:

我正在尝试通过此链接确定客户端的 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);
>     } } 

注意事项:

  1. 此属性仅适用于 http 和 tcp 传输。在所有其他 诸如 MSMQ 和 NamedPipes 之类的传输,此属性将不会 可用的。
  2. 地址和端口由socket或http.sys报告 服务的机器。因此,如果客户端通过 VPN 或某些 修改地址的其他代理,新地址将是 表示而不是客户端的本地地址。这是可取的 很重要,因为这是服务的地址和端口 将客户视为客户,而不是客户将自己视为客户。这也意味着 可能会有一些欺骗行为。客户或其他东西 客户端和服务器之间可能会欺骗地址。所以,不要使用 任何安全决策的地址或端口,除非您添加一些 其他自定义检查机制。
  3. 如果您在您的 服务,那么服务不仅会填充此属性 对于客户端,但客户端也将填充此属性 为来自该服务的每个调用的服务。

我有 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();
    ...
} 

【问题讨论】:

  • 您可能想展示您尝试过但没有奏效的方法。
  • 我同意詹姆斯的观点。我不明白为什么这对 WebInvoke 不起作用。您能否提供一些代码(和配置)以便我们了解您的处理方式?
  • 谢谢。我已经添加了一些代码如上所示。
  • 如果这不起作用,您应该创建一个简单的示例来重现问题并将其发布到 Microsoft Connect。对我来说,这是一个错误,因为没有理由 GET 应该工作而 POST 不能。
  • 链接已失效,因此很难引用代码现在应该做什么。

标签: wcf client ip-address webinvoke


【解决方案1】:

您是否尝试过使用 HttpContext?它并非在所有 WCF 模式下都可用,但这取决于您的环境:

if (HttpContext.Current != null)
            {
                Trace.WriteLine(
                    "Who's calling? IP address: '{0}', Name: '{1}', User Agent: '{2}', URL: '{3}'.",
                    HttpContext.Current.Request.UserHostAddress, HttpContext.Current.Request.UserHostName,
                    HttpContext.Current.Request.UserAgent, HttpContext.Current.Request.Url);
            }

【讨论】:

    猜你喜欢
    • 2010-09-10
    • 2019-01-14
    • 2019-11-13
    • 1970-01-01
    • 2013-07-07
    • 2011-05-26
    • 2015-03-01
    • 2021-05-14
    • 2011-01-08
    相关资源
    最近更新 更多