Sunday, April 11, 2010

.Net 2 remoting with security support, simplified

I tried a complicated way to initialize the channels to make the caller's identity pass through wire from client to the server. If you need to really impersonate the client or even delegate functions on behalf of the client user, then you have go through that tedious way. I don't like it. The parameters are in plain string and you have to make sure no typo is made. (What's wrong with a strong typed parameter objects?)

Anyway, what all I need to is to know which user is calling some function. I don't need to impersonate or delegation. So there is a simpler way:

On server, just use
IChannel tsc = new TcpServerChannel(8888);
ChannelServices.RegisterChannel(tsc, true);
Note you have to specify ture when you register the channel. Then you are ready to publish objects, either by RemotingConfiguration.RegisterWellKnownServiceType, or by RemotingServices.Marshal.
At client side, you MUST register a TcpClientChannel also with true to the 2nd parameter before activating the server objects. If you forget this, or put false, the client will not be connected correctly.
TcpClientChannel tcp = new TcpClientChannel();
ChannelServices.RegisterChannel(tcp, true);
_Server = (IService)Activator.GetObject(typeof(IService), "tcp://localhost:8888/ASvc");

That's it, no ugly hashtable for parameter passing and still you can pass the caller's identity to server.

No comments:

Post a Comment