.NET Remoting Security使用小结 – TcpChannel

 

.NET Remoting Security使用小结 – TcpChannel

谈到Security 需要从下面四个方面考虑:

1.       Authentication:防止非法用户的调用。

2.       Authorization:防止合法但权限不够的用户调用。

3.       Encryption:防止数据在传输过程中被窃取。

4.       Sign:防止数据在传输过程被篡改。

 

下面就谈谈.NET Remoting是如何满足上面四个方面要求的。.NET Remoting有三种Channel可供选择:HttpChannelTcpChannelIpcChannel。它们实现Security的方式不完全相同。这次只谈TcpChannel是如何实现的。基本来说就是对该Channel一系列属性的设置。.NET Remoting属性设置有两种方式,为了都说明到,下面对Server端的设置采用代码方式,对Client端采用配置文件方式。

 

1.       Server端:

1)         首先将secure属性设置为true。这样缺省情况下在Server端和Client端传输的数据就是经过认证、加密且签名的。

2)         如果觉得上面的缺省行为有点过度保护,影响了数据传输的性能。可以设置protectionLevel属性来调整。该属性有NoneSignEncryptAndSign三个可选值。在secure属性设置为true时,它的缺省值是EncryptAndSign。可以将该值设为其它两种之一。需要注意的是该值的设定,在Client端一定要与Server端相同或至少包含Server端的设定(如Server端设SignClientEncryptAndSign),否则会报错“A remote side security requirement was not fulfilled during authentication. Try increasing the ProtectionLevel and/or ImpersonationLevel.”,

3)         通过设置authorizationModule属性来对用户授权,即验证了用户的合法性后,决定是否允许该用户调用远程对象提供的函数。该属性的值比较复杂,需要提供一个实现了IAuthorizeRemotingConnection接口的类名和该类所在的Assembly名。通过实现该接口的IsConnectingIdentityAuthorized()函数决定允许哪些用户可以调用调用远程对象提供的函数。

4)         如果希望以Client提供的用户的名义来调用远程对象提供的函数,可以将impersonate属性设置为true

5)         通过ChannelServices.RegisterChannel()注册端口时,第二个参数传true

 

2.       Client端:

1)         secureprotectionLevel属性设置为与Server端一样。

2)         如果Server端将impersonate属性设置为true,相应的Client端需要将tokenImpersonationLevel属性设置为impersonation。注意这个属性在两边的名字和值是不完全一样的。

3)         缺省情况下Client端的用户就是运行该进程的用户。如果希望用其它的用户去接受Security检查,需要设置domainusernamepassword属性。

4)         通过RemotingConfiguration.Configure()应用配置文件中的设置时第二个参数传true

 

3.       示例代码:

1)         Server端:

a.实现IAuthorizeRemotingConnection接口:

class AuthorizationModule : IAuthorizeRemotingConnection

        {

public bool IsConnectingIdentityAuthorized(System.Security.Principal.IIdentity identity)

              {

                   Console.WriteLine(identity.Name + " Called!");

Console.WriteLine("Is Authenticated? " + identity.IsAuthenticated);           

return true;

              }       

 }

b.设置Channel属性:

Dictionary<string, string> properties = new Dictionary<string, string>();

         properties["secure"] = "true";

         properties["port"] = "8001";

         properties["impersonate"] = "true";

         properties["protectionLevel"] = "Sign";

properties["authorizationModule"] = "Spacer_Robot.RemotingTest.AuthorizationModule,HelloServer";

         TcpServerChannel tcpChannel = new TcpServerChannel(properties, null);           

ChannelServices.RegisterChannel(tcpChannel, true);

2)         Client端:

a.在App.config中的配置:

                 <?xml version="1.0" encoding="utf-8" ?>

<configuration>

                  <system.runtime.remoting>

                       <application>

                            <channels>

                                 <channel ref="tcp"

                                     secure="true"

                                     tokenImpersonationLevel="impersonation"

                                     protectionLevel="Sign"

                                     domain="xxxx"

                                     username="xxxx"

                                     password="xxxx"/>

                            </channels>

                        </application>

                  </system.runtime.remoting>

</configuration>

           b.应用该配置的代码:

                   RemotingConfiguration.Configure("HelloClient.exe.config", true);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值