我可以为 WCF 服务设置 IP 过滤器吗? [英] Can I setup an IP filter for a WCF Service?

查看:44
本文介绍了我可以为 WCF 服务设置 IP 过滤器吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在修改我的 WCF API 以包含一个应仅向内部 IP 地址公开的新服务.我的 API 中的所有服务都可以在 SOAP、POX 和 JSON 中使用.我正在寻找的是一种行为或允许我实现简单的 IP 地址过滤器的东西,以处理来自内部 IP 的请求并拒绝其他一切.我希望它在配置中工作,因为 API 中的所有其他服务都应该对 Internet 保持可用.我做了一些谷歌搜索,但找不到 WCF 内置的任何东西.我错过了什么吗?

I'm modifying my WCF API to include a new service that should be exposed to internal IP addresses only. All of the services in my API are available in SOAP, POX and JSON. What I'm looking for is a behavior or something that allows me to implement a simple IP address filter, to process requests from internal IP's and deny everything else. I'd like it to work in configuration, because all the other services in the API should remain available to the Internet. I did some googling but can't find anything like this built into WCF. Am I missing something?

推荐答案

好的,我想通了,在我看来,它有点巧妙.我将 IP 过滤器系统实现为服务行为,然后将其添加到 web.config 中的服务中.这是我的新网络配置行为部分:

Ok, I figured it out, and its kind of slick, in my opinion. I implemented an IP Filter system as a service behavior, then added it to my service in the web.config. Here's my new web config behaviors section:

<serviceBehaviors>
    <behavior name="ServiceBehaviour">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
    <behavior name="RestrictedServiceBehaviour">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
      <IPFilter filter="172.*.*.* 127.0.0.1" />          
    </behavior>
  </serviceBehaviors>

IPFilter 类实现 IDispatchMessageInspector 以尽快捕获请求,检查客户端 IP 并在与过滤器不匹配时抛出异常.如果有人感兴趣,我可以发布我的代码.

The IPFilter class implements IDispatchMessageInspector to catch the request as soon as possible, inspect the client IP and throw an exception if it doesn't match the filter. If anyone's interested I can post my code.

这篇关于我可以为 WCF 服务设置 IP 过滤器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆