WCF 3.5 终于能获取客户端信息了,现在我们可以为 "Self Hosting"、"Windows Services Hosting" 补充一些额外的功能。
class BlockIP
{
static BlockIP()
{
Instance = new BlockIP();
}
private BlockIP()
{
IPs = new HashSet<string>();
}
public static BlockIP Instance { get; private set; }
public HashSet<string> IPs { get; private set; }
internal void Check()
{
var context = OperationContext.Current;
var remote = context.IncomingMessageProperties[RemoteEndpointMessageProperty.Name] as
RemoteEndpointMessageProperty;
if (this.IPs.Contains(remote.Address))
{
OperationContext.Current.InstanceContext.Close();
}
}
}
[ServiceContract]
interface IMyService
{
[OperationContract]
void Test();
}
class MyService : IMyService
{
public void Test()
{
BlockIP.Instance.Check();
Console.WriteLine("test...");
}
}
class Program
{
static void Main(string[] args)
{
Server();
Client();
Console.WriteLine("Press any key to exit...");
Console.ReadKey(true);
Environment.Exit(0);
}
private static void Client()
{
var binding = new BasicHttpBinding();
var address = new EndpointAddress("http://localhost:90");
var channel = ChannelFactory<IMyService>.CreateChannel(binding, address);
using (channel as IDisposable)
{
channel.Test();
channel.Test();
}
}
private static void Server()
{
AppDomain.CreateDomain("Server").DoCallBack(() =>
{
BlockIP.Instance.IPs.Add("127.0.0.1");
var host = new ServiceHost(typeof(MyService), new Uri("http://localhost:90"));
host.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = true });
host.AddServiceEndpoint(typeof(IMyService), new BasicHttpBinding(), "");
host.Open();
});
}
}
{
static BlockIP()
{
Instance = new BlockIP();
}
private BlockIP()
{
IPs = new HashSet<string>();
}
public static BlockIP Instance { get; private set; }
public HashSet<string> IPs { get; private set; }
internal void Check()
{
var context = OperationContext.Current;
var remote = context.IncomingMessageProperties[RemoteEndpointMessageProperty.Name] as
RemoteEndpointMessageProperty;
if (this.IPs.Contains(remote.Address))
{
OperationContext.Current.InstanceContext.Close();
}
}
}
[ServiceContract]
interface IMyService
{
[OperationContract]
void Test();
}
class MyService : IMyService
{
public void Test()
{
BlockIP.Instance.Check();
Console.WriteLine("test...");
}
}
class Program
{
static void Main(string[] args)
{
Server();
Client();
Console.WriteLine("Press any key to exit...");
Console.ReadKey(true);
Environment.Exit(0);
}
private static void Client()
{
var binding = new BasicHttpBinding();
var address = new EndpointAddress("http://localhost:90");
var channel = ChannelFactory<IMyService>.CreateChannel(binding, address);
using (channel as IDisposable)
{
channel.Test();
channel.Test();
}
}
private static void Server()
{
AppDomain.CreateDomain("Server").DoCallBack(() =>
{
BlockIP.Instance.IPs.Add("127.0.0.1");
var host = new ServiceHost(typeof(MyService), new Uri("http://localhost:90"));
host.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = true });
host.AddServiceEndpoint(typeof(IMyService), new BasicHttpBinding(), "");
host.Open();
});
}
}
如果客户端的 IP 被添加到阻止列表,那么他只能看到下面这个东东。
当然,你也可以反过来写允许被接入的 IP 列表。
本文介绍如何使用WCF 3.5实现客户端IP地址的过滤功能,通过自定义行为检查连接请求,并阻止指定IP地址的访问。示例代码展示了如何在服务端设置阻止列表及客户端调用。

3637

被折叠的 条评论
为什么被折叠?



