July 25, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Improved .NET Remoting, Part 2: Secure TCP

  • July 25, 2005
  • By Mark Strawmyer
  • Send Email »
  • More Articles »

Putting It All Together to Test the IPC Remoting Sample

Once you have created the projects and successfully compiled each of them, you are ready to try it out. Start the server by running the compiled executable. After the server successfully starts, console window will display with the message "Press the enter key to exit..." (see Figure 1).



Click here for a larger image.

Figure 1. The Server Successfully Starts

The server is listening, so you are now ready to run the client. Executing the client, whether by running the executable or debugging in Visual Studio, should result in "Hello World!" being displayed in a separate console window (see Figure 2). The window will pause until you press the Enter key in order to allow you time to see the resulting output message.



Click here for a larger image.

Figure 2.The Client Successfully Executes

You should change some of the sample code. For example, if you change the client tokenImpersonationLevel to "Identification" and run the code, you'll receive an exception because the authentication configuration of the client and server do not match.

Using Application Configuration Files

Setting up the security in the application code is all well and good. However, I personally would prefer not to have to change code in order to make modifications to the way the security behaves in communication. This is where the application configuration files come in to the picture. You can accomplish the same thing you did in the parameter setup of both the client and the server through the use of configuration files. You can set up and control the entire connection through configuration files. Just replace the code for configuring and setting up the channel and move it into the configuration files.

RemotingServerSample Configuration File

Add an application configuration file to your server. The following is the App.config file:
<?xml version="1.0" encoding="utf-8" ?><configuration>  <system.runtime.remoting>    <application>      <service>        <wellknown mode="Singleton" type="CodeGuru.RemotingSample.SampleObject, RemotingSample" objectUri="HelloWorld.rem" />      </service>      <channels>        <channel ref="tcp" secure="true" port="8080" impersonate="true" />      </channels>    </application>  </system.runtime.remoting></configuration>

The wellknown element specifies your mode, the object to create, the assembly location, and the URI to assign it. The channel element specifies that you want secure communication, to use port 8080, and to impersonate the user.

The server code becomes much simpler because the channel setup is now pushed into the configuration file:

using System;using System.Runtime.Remoting;using System.Runtime.Remoting.Channels;using System.Runtime.Remoting.Channels.Tcp; namespace CodeGuru.RemotingSample{  /// <remarks>  /// Sample server to demonstrate the use of secure .NET Remoting.  /// </remarks>  public class SampleRemotingServer  {      public static void Main()       {  // Set the configuration according to your config file        RemotingConfiguration.Configure("RemotingClientSample.exe.config");          Console.WriteLine("Listening on {0}", ChannelServices.RegisteredChannels[0].ChannelName);  Console.WriteLine("Press the enter key to exit...");        Console.ReadLine();      }  }}

RemotingClientSample Configuration File

Add an application configuration file to your client. The App.config file should contain the following:
<?xml version="1.0" encoding="utf-8" ?><configuration>  <system.runtime.remoting>    <application>      <channels>        <channel ref="tcp" secure="true" tokenImpersonationLevel="impersonation" />      </channels>    </application>  </system.runtime.remoting></configuration>

The client code becomes much simpler because the channel setup is now pushed into the configuration file:

using System;using System.Runtime.Remoting;using System.Runtime.Remoting.Channels;using System.Runtime.Remoting.Channels.Ipc; namespace CodeGuru.RemotingSample{  /// <remarks>  /// Sample client to demonstrate the use of secure .NET Remoting.  /// </remarks>  public class SampleRemotingClient  {      public static void Main()      {  // Set the configuration according to your config file        RemotingConfiguration.Configure("RemotingClientSample.exe.config");          // Create an instance of the remote object        RemotingConfiguration.RegisterWellKnownClientType(            typeof(SampleObject),            "ipc://localhost:8080/HelloWorld.rem");        SampleObject sample = new SampleObject();    Console.WriteLine("{0}", sample.HelloWorld());  Console.WriteLine("Press the enter key to exit...");        Console.ReadLine();      }   }}

A Step Towards Secure Computing

.NET remoting is a powerful way to enable interprocess communication. The security enhancements to the Runtime.Remoting.Channels.Tcp namespace are a welcome step towards secure computing. It helps to ensure the objects flowing between your applications are secure during transport.

Future Columns

The topic of the next column is yet to be determined. If you have something in particular that you would like to see explained here, contact me at mstrawmyer@crowechizek.com.

About the Author

Mark Strawmyer (MCSD, MCSE, MCDBA) is a senior architect of .NET applications for large and mid-size organizations. Mark is a technology leader with Crowe Chizek in Indianapolis, Indiana. He leads a group that specializes in architecture, design, and development of Microsoft-based solutions. Mark was honored to be named a Microsoft MVP for application development with C# for the second year in a row. You can reach Mark at mstrawmyer@crowechizek.com.



Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel