Remoting系列专题---构建Remoting“防火墙”

    做项目不得不考虑安全问题,但是在Remoting应用中我似乎没有找到象WebService那样现成可用的防止非授权人员随意调用的方法和验证安全机制。当然,简单一点的可以通过Remoting客户端传输一些验证代码,然后在服务端验证这些代码的合法性,以实现是否安全授权调用,但是总觉得这样比较麻烦。我也见有些人主动把客户端的IP地址获取后主动送入到Remotin g服务端验证以便识别是否合法,其实原理跟上面的是一样的道理,我觉得这样传送很容易让别人仿造一些数据送入,从而轻易获得合法调用。当然以上数据可以通过把数据特殊的加密和解密也能得到比较好的安全性。在这里我是想介绍另外一种安全机制,即构建Remoting“防火墙”。
     其实我所说的Remoting“防火墙”也是最基本的。由于Remoting的TcpChannel没有提供内建的认证机制,所以没有现成获取客户端的方法,我们可以在Remoting Server端注册上自定义的Server Channel Sink,通过Transport Headers来获取request的IP,以下是自定义的Server Channel Sink类的代码,( 注:原代码不是我写的,原出处我忘记了,所以无法标注来源
ContractedBlock.gif ExpandedBlockStart.gif
  1None.gifusing System;
  2None.gifusing System.Collections;
  3None.gifusing System.IO;
  4None.gifusing System.Runtime.Remoting;
  5None.gifusing System.Runtime.Remoting.Messaging;
  6None.gifusing System.Runtime.Remoting.Channels;
  7None.gifusing System.Threading;
  8None.gifusing System.Net;
  9None.gif
 10None.gif
 11None.gifnamespace Colorful.RemoteObject
 12ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 13InBlock.gif    public class ClientIPServerSinkProvider : IServerChannelSinkProvider
 14ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 15InBlock.gif
 16InBlock.gif        private IServerChannelSinkProvider next = null;
 17InBlock.gif
 18InBlock.gif        public ClientIPServerSinkProvider()
 19ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 20ExpandedSubBlockEnd.gif        }

 21InBlock.gif
 22InBlock.gif        public ClientIPServerSinkProvider(IDictionary properties, ICollection providerData)
 23ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 24ExpandedSubBlockEnd.gif        }

 25InBlock.gif
 26InBlock.gif        public void GetChannelData(IChannelDataStore channelData)
 27ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 28ExpandedSubBlockEnd.gif        }

 29InBlock.gif
 30InBlock.gif        public IServerChannelSink CreateSink(IChannelReceiver channel)
 31ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 32InBlock.gif            IServerChannelSink nextSink = null;
 33InBlock.gif
 34InBlock.gif            if (next != null)
 35ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 36InBlock.gif                nextSink = next.CreateSink(channel);
 37ExpandedSubBlockEnd.gif            }

 38InBlock.gif
 39InBlock.gif            return new ClientIPServerSink(nextSink);
 40ExpandedSubBlockEnd.gif        }

 41InBlock.gif
 42InBlock.gif
 43InBlock.gif
 44InBlock.gif        public IServerChannelSinkProvider Next
 45ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 46ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn next; }
 47ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ next = value; }
 48ExpandedSubBlockEnd.gif        }

 49ExpandedSubBlockEnd.gif    }

 50InBlock.gif
 51InBlock.gif    public class ClientIPServerSink : BaseChannelObjectWithProperties, IServerChannelSink, IChannelSinkBase
 52ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 53InBlock.gif        private IServerChannelSink _next;
 54InBlock.gif
 55InBlock.gif        public ClientIPServerSink(IServerChannelSink next)
 56ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 57InBlock.gif            _next = next;
 58ExpandedSubBlockEnd.gif        }

 59InBlock.gif
 60InBlock.gif        public void AsyncProcessResponse(System.Runtime.Remoting.Channels.IServerResponseChannelSinkStack sinkStack, System.Object state, System.Runtime.Remoting.Messaging.IMessage msg, System.Runtime.Remoting.Channels.ITransportHeaders headers, System.IO.Stream stream)
 61ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 62ExpandedSubBlockEnd.gif        }

 63InBlock.gif
 64InBlock.gif        public Stream GetResponseStream(System.Runtime.Remoting.Channels.IServerResponseChannelSinkStack sinkStack, System.Object state, System.Runtime.Remoting.Messaging.IMessage msg, System.Runtime.Remoting.Channels.ITransportHeaders headers)
 65ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 66InBlock.gif            return null;
 67ExpandedSubBlockEnd.gif        }

 68InBlock.gif
 69InBlock.gif        public System.Runtime.Remoting.Channels.ServerProcessing ProcessMessage(System.Runtime.Remoting.Channels.IServerChannelSinkStack sinkStack, System.Runtime.Remoting.Messaging.IMessage requestMsg, System.Runtime.Remoting.Channels.ITransportHeaders requestHeaders, System.IO.Stream requestStream, out System.Runtime.Remoting.Messaging.IMessage responseMsg, out System.Runtime.Remoting.Channels.ITransportHeaders responseHeaders, out System.IO.Stream responseStream)
 70ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 71InBlock.gif            if (_next != null)
 72ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 73InBlock.gif                IPAddress ip = requestHeaders[CommonTransportKeys.IPAddress] as IPAddress;
 74InBlock.gif
 75InBlock.gif                CallContext.SetData("IP", ip);
 76InBlock.gif
 77InBlock.gif                ServerProcessing spres = _next.ProcessMessage(sinkStack, requestMsg, requestHeaders, requestStream, out responseMsg, out responseHeaders, out responseStream);
 78InBlock.gif
 79InBlock.gif                return spres;
 80ExpandedSubBlockEnd.gif            }

 81InBlock.gif            else
 82ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 83InBlock.gif                responseMsg = null;
 84InBlock.gif
 85InBlock.gif                responseHeaders = null;
 86InBlock.gif
 87InBlock.gif                responseStream = null;
 88InBlock.gif
 89InBlock.gif                return new ServerProcessing();
 90ExpandedSubBlockEnd.gif            }

 91ExpandedSubBlockEnd.gif        }

 92InBlock.gif
 93InBlock.gif
 94InBlock.gif
 95InBlock.gif        public IServerChannelSink NextChannelSink
 96ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 97ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn _next; }
 98ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ _next = value; }
 99ExpandedSubBlockEnd.gif        }

100ExpandedSubBlockEnd.gif    }

101ExpandedBlockEnd.gif}

102None.gif
以下是 远程对象主程序代码
ContractedBlock.gif ExpandedBlockStart.gif
 1None.gifclass Program
 2ExpandedBlockStart.gifContractedBlock.gif    dot.gif{
 3InBlock.gif        static void Main(string[] args)
 4ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 5InBlock.gif            int TcpPort = ConfigInfo.RemoteSocketPort;
 6InBlock.gif            BinaryServerFormatterSinkProvider provider = new BinaryServerFormatterSinkProvider();
 7InBlock.gif            provider.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
 8InBlock.gif            //实例化自定义的Server Channel Sink类
 9InBlock.gif            Colorful.RemoteObject.ClientIPServerSinkProvider IpInjProvider = new Colorful.RemoteObject.ClientIPServerSinkProvider();
10InBlock.gif            provider.Next = IpInjProvider;//加入接收链
11InBlock.gif            
12InBlock.gif            IDictionary props = new Hashtable();
13InBlock.gif            props["port"= TcpPort;
14InBlock.gif            
15InBlock.gif            TcpChannel chan1 = new TcpChannel(props,null,provider);
16InBlock.gif
17InBlock.gif            ChannelServices.RegisterChannel(chan1, false);
18InBlock.gif
19InBlock.gif            LifetimeServices.LeaseTime = TimeSpan.Zero;//租用周期
20InBlock.gif            RemotingConfiguration.RegisterWellKnownServiceType
21InBlock.gif                (
22InBlock.gif                typeof(Colorful.RemoteObject.Server),
23InBlock.gif                ConfigInfo.RemoteSocketURI,
24InBlock.gif                WellKnownObjectMode.Singleton
25InBlock.gif                );
26InBlock.gif            Console.WriteLine(DateTime.Now.ToString() + "服务通道注册成功!等待数据服务请求dot.gifdot.gif");            
27InBlock.gif
28InBlock.gif            Console.ReadLine();
29ExpandedSubBlockEnd.gif        }

30ExpandedBlockEnd.gif    }
然后通过配置许可的IP地址XML文件,读取XML验证IP地址列表,然后调用以下函数验证客户端调用的合法性,非授权的IP拒绝访问
ContractedBlock.gif ExpandedBlockStart.gif   验证客服端的合法性 #region 验证客服端的合法性
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 检查Remoting客户端的合法性
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public bool CheckRemotingClient()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            IPAddress data 
= (IPAddress)CallContext.GetData("ClientIPAddress");
InBlock.gif
InBlock.gif            
if (clientIPAddress.ContainsKey(data))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string msg = "接受远程地址:" + data.ToString() + "的服务请求";
InBlock.gif                Console.WriteLine(msg);
InBlock.gif                
return true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string msg = "远程地址:" + data.ToString() +"不在许可范围内,服务请求被拒绝";
InBlock.gif                Console.WriteLine(msg);
InBlock.gif                SQL2005DAL.CommDAL.WriteLogFile(
"RemotingClientTrace", msg, "Warring"false);
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif        
#endregion
基本上就这么多,详细内容,请参看 VB.NET Remoting技术手册,其他安全请参看. NET Remoting 安全性

转载于:https://www.cnblogs.com/blockhead/archive/2006/08/29/489528.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值