天天看点

.NET Remoting安全性问题

在.NET framework 1.1中,只支持在IIS为宿主,并使用HTTP信道时才可以有内置的安全支持。而使用TCP信道时,则要自己写程序才可以。 不过,这在.NET Framework 2.0中得到了改进。现在TCP Channel也可以实现内置的安全功能。不过,MSDN给出的文档很少。都不知道是什么原理以及如何有效的使用。不知道现在的网上有没有什么大侠可以指点一下。呵呵。

在以前的MSDN杂志有两篇文章介绍了一下.net Remoting的安全解决方案。现在还没有看完。希望有点帮助吧

UPdate 2006-05-08

经过测试,了解了一些TCP Channel配置属性。在MSDN中也说明了。只是我没有试过,也没有讲过具体的运作方式,所有心里没有底。

在写了一个例子程序,然后将Remoting 客户端与服务器端的TcpChannel的配置加入属性。

客户端代码:

                IDictionary myDic = new Hashtable();

                myDic["name"] = "MyTcpClient";

                myDic["port"] = 0;

                myDic["impersonationLevel"] = "Identify";

                myDic["tokenImpersonationLevel"] = "Identification";

                myDic["secure"] = true; //进行加密标识,上面两个标识是加密所必须的。进行证书交换

                myDic["username"] = "guest"; // 在不以域组的网络上,如果不加用户名与密码时,会出现认证不过的情况

                myDic["password"] = "";

                BinaryServerFormatterSinkProvider bsp = new BinaryServerFormatterSinkProvider();

                bsp.TypeFilterLevel = TypeFilterLevel.Full;

                BinaryClientFormatterSinkProvider bcp = new BinaryClientFormatterSinkProvider();

                TcpChannel chnl = new TcpChannel(myDic, bcp, bsp);

                ChannelServices.RegisterChannel(chnl, false);

                myHello = (CardGameLib.HelloWorld)Activator.GetObject(typeof(CardGameLib.HelloWorld),

                    "tcp://localhost:2234/HelloWorld.rem");

服务器代码要简单一点。

            IDictionary myDic = new Hashtable();

            myDic["port"] = 2234;

            myDic["name"] = "MyTcpServer";

            myDic["authenticationMode"] = "identifyCallers";

            myDic["tokenImpersonationLevel"] = "Identification";

            myDic["secure"] = true;

            BinaryServerFormatterSinkProvider bsp = new BinaryServerFormatterSinkProvider();

            bsp.TypeFilterLevel = TypeFilterLevel.Full;

            BinaryClientFormatterSinkProvider bcp = new BinaryClientFormatterSinkProvider();

            TcpChannel chnl = new TcpChannel(myDic, bcp, bsp);

            ChannelServices.RegisterChannel(chnl, false);

            RemotingConfiguration.RegisterWellKnownServiceType(typeof(MyHelloWorld),

                "HelloWorld.rem", WellKnownObjectMode.Singleton);