识别 WCF 客户端 ID

发布于 2024-09-10 02:05:30 字数 197 浏览 4 评论 0原文

我有一个公开多种业务方法的 WCF Web 服务。我还有两个客户端 - 一个 asp.net GUI 和一个数据迁移应用程序,它们都连接到 wcf 后端以调用各种业务事务。

我需要我的后端能够识别和区分哪个 wcf 客户端调用了某些变体逻辑。

有没有办法让我的 WCF 服务能够识别连接到它的客户端?还有没有办法使用签名密钥来防止客户端欺骗其身份?

I have a WCF web service that exposes several business methods. I also have two clients - an asp.net GUI and a data migration application that both connect to the wcf backend to invoke various business transactions.

I need my backend to be able to identify and distinguish between which wcf client has made a call to some variant logic.

Is there a way that my WCF service is able to identify clients connected to it? Also is there a way to use a signed key to prevent a client from spoofing their identity?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

铁憨憨 2024-09-17 02:05:30

您可以通过自定义标头解决此问题。

您可以添加自定义标头作为客户端应用程序配置文件中端点的一部分。然后,您可以使每个客户端的自定义标头不同。例如,在 ASP.NET 版本中:

        <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
   {
      // ...
   }
}

You can solve this via a custom header.

You can add a custom header as part of the endpoint in the client application's configuration file. You would then make each client's custom header different. For example, in the ASP.NET version:

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

Then the service can check the header value like so:

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
   {
      // ...
   }
}
风柔一江水 2024-09-17 02:05:30

为了识别调用者的类型(ASP.NET 与 WInforms 或其他),您可能需要向 WCF 消息添加自定义标头 - 服务无法了解有关调用客户端的任何信息,除非它是消息的一部分或发送的标头。为此,最好的选择是编写一个 WCF 消息检查器 - 以及这个 此处的博客文章将向您展示如何执行此操作。

至于安全性 - 取决于您的环境。在防火墙后面的公司 LAN 中 - 使用 Windows 凭据。如果您“面向外部”,最好的选择是在客户端上安装数字证书以验证其身份。

WCF 大师 Juval Löwy 在 MSDN 杂志上发表了一篇非常好的文章,声明性 WCF 安全,描述了 WCF 中的五种常见安全场景以及如何实现它们。强烈推荐阅读。

In order to identify the type of caller (ASP.NET vs. WInforms or whatever), you probably need to add a custom header to your WCF messages - there's no way the service can know anything about the calling client unless it's part of the message or the headers sent. For this, your best bet is to write a WCF Message Inspector - and this blog post here will show you how to do this.

As for security - depends on your environment. In a corporate LAN behind a firewall - use the Windows credentials. If you're "outside facing", your best bet would be to install digital certificates on the clients to verify their identity.

WCF Guru Juval Löwy has a really good article on MSDN Magazine, Declarative WCF Security, that describes five common security scenarios in WCF and how to implement them. Highly recommended reading.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文