.NET Remoting Security使用小结 – TcpChannel
1. Authentication:防止非法用户的调用。
2. Authorization:防止合法但权限不够的用户调用。
3. Encryption:防止数据在传输过程中被窃取。
4. Sign:防止数据在传输过程被篡改。
下面就谈谈.NET Remoting是如何满足上面四个方面要求的。.NET Remoting有三种Channel可供选择:HttpChannel、TcpChannel和IpcChannel。它们实现Security的方式不完全相同。这次只谈TcpChannel是如何实现的。基本来说就是对该Channel一系列属性的设置。.NET Remoting属性设置有两种方式,为了都说明到,下面对Server端的设置采用代码方式,对Client端采用配置文件方式。
1. Server端:
1) 首先将secure属性设置为true。这样缺省情况下在Server端和Client端传输的数据就是经过认证、加密且签名的。
2) 如果觉得上面的缺省行为有点过度保护,影响了数据传输的性能。可以设置protectionLevel属性来调整。该属性有None、Sign和EncryptAndSign三个可选值。在secure属性设置为true时,它的缺省值是EncryptAndSign。可以将该值设为其它两种之一。需要注意的是该值的设定,在Client端一定要与Server端相同或至少包含Server端的设定(如Server端设Sign,Client设EncryptAndSign),否则会报错“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) 将secure和protectionLevel属性设置为与Server端一样。
2) 如果Server端将impersonate属性设置为true,相应的Client端需要将tokenImpersonationLevel属性设置为impersonation。注意这个属性在两边的名字和值是不完全一样的。
3) 缺省情况下Client端的用户就是运行该进程的用户。如果希望用其它的用户去接受Security检查,需要设置domain、username和password属性。
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);
posted on 2008-11-29 20:36 spacer_robot 阅读(4060) 评论(2) 编辑 收藏 举报