代码之家  ›  专栏  ›  技术社区  ›  Hady

识别WCF客户端ID

  •  7
  • Hady  · 技术社区  · 16 年前

    有没有办法让我的WCF服务能够识别连接到它的客户端?还有一种方法可以使用签名密钥来防止客户机欺骗他们的身份吗?

    2 回复  |  直到 16 年前
        1
  •  15
  •   Andrew Shepherd    16 年前

            <endpoint
                name="basicHttpEndpoint"
                address="http://localhost:8972"
                binding="basicHttpBinding"
                contract="MySeriveContractLib.IMyService"
                >
                <headers>
                    <ClientIdentification>ASP_Client</ClientIdentification>
                </headers>
            </endpoint>
    

    public void MyServiceMethod()
    {
       var opContext = OperationContext.Current;
       var requestContext = opContext.RequestContext;
       var headers = requestContext.RequestMessage.Headers;
       int headerIndex = headers.FindHeader("ClientIdentification", "");
       var clientString = headers.GetHeader<string>(headerIndex);
       if clientString=="ASP_Client"
       {
           // ...
       }
       else
       {
          // ...
       }
    }
    
        2
  •  3
  •   marc_s MisterSmith    16 年前

    为了识别调用方的类型(ASP.NET与WInforms或其他什么),您可能需要向WCF消息添加一个自定义头-除非它是消息的一部分或发送的头,否则服务无法知道有关调用客户端的任何信息。为此,您最好的选择是编写一个WCF消息检查器—以及 blog post here 我会教你怎么做。

    WCF大师Juval Lwy在MSDN杂志上有一篇很好的文章, Declarative WCF Security ,描述了WCF中五种常见的安全场景以及如何实现它们。强烈推荐阅读。