Click here to Skip to main content
15,891,248 members
Articles / Programming Languages / C#

Dynamic/Transparent Proxy using DynamicProxy.NET

Rate me:
Please Sign up or sign in to vote.
4.90/5 (19 votes)
21 Oct 20039 min read 208.9K   3.9K   78  
Learn how easy it is to create Dynamic/Transparent proxies in .NET using DynamicProxy.NET
using System;
using System.Reflection;
using System.Security.Principal;
using Cramon.NetExtension.DynamicProxy;

namespace DynamicProxyTutorial
{
	/// <summary>
	/// Class the encapsulates that logic for checking permissions on a dynamic proxy.
	/// This class utilizes the DynamicProxy.NET framework to create a dynamic proxy for a FileViewer.
	/// The reason why we need to encapsulate this, is that we need a place to place the allowedUserID and
	/// the invocation handler delegate.
	/// </summary>
	public class DynamicFileViewerProxy {
		private IDynamicProxy dynamicFileViewerProxy;
		private string allowedUserID;

		public DynamicFileViewerProxy(string filePath, string allowedUserID) {
			this.allowedUserID = allowedUserID;

			// Create the real FileViewer that we wish to proxy
			IFileViewer realViewer = new FileViewer(filePath);
			// Create a dynamic proxy for the real FileViewer.
			// DynamicProxy.NET will automatically create a dynamic proxy instance that
			// implements the methods that FileViewer exposes.
			dynamicFileViewerProxy = (IDynamicProxy)DynamicProxyFactory.Instance.CreateProxy(realViewer,
															  new InvocationDelegate(invocationHandler));
		}

		public IFileViewer Proxy {
			get {
				return (IFileViewer)dynamicFileViewerProxy;
			}
		}

		/// <summary>
		/// Checks if the current user is the same as the allowed user. 
		/// If it isn't a SecurityException is thrown. (same as in FileViewerProxy)
		/// </summary>
		/// <exception cref="SecuritException">If the current user isn't allowed to 
		/// read/write from the file, this exception is thrown</exception>
		private void checkPermission() {
			if (WindowsIdentity.GetCurrent().Name != allowedUserID) {
				// Get a hold of the proxy we're proxying for
				IFileViewer realFileViewer = (IFileViewer)dynamicFileViewerProxy.ProxyTarget;
				throw new SecurityException("From : " + this.GetType().Name + "\r\nUserID '" 
					+ WindowsIdentity.GetCurrent().Name + 
					" isn't allowed to read/write from the file: '" + realFileViewer.FilePath + "'. " + 
					"Allowed user: '" + allowedUserID + "'");
			}
		}


		private object invocationHandler(object target, MethodBase method, object[] parameters) {
			// Target contains the object that we are proxying (for this example to work, it's not actually
			// necessary to cast the target to the real type). With the implementation we have made here we
			// already know the target object (because we store it in the dynamicFileViewerProxy). The reason
			// why the target is provided is that it makes it possible to create really generic invocationHandlers
			// which can handle invocations for many different target objects.
			IFileViewer realViewer = (IFileViewer)target;
			
			// Do the work of our protection proxy (but only for the ReadFromFile method)
			if (method.Name == "ReadFromFile") {
				checkPermission();
			}

			// Call the method the user requested on the real target 
			return method.Invoke(realViewer, parameters);
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Denmark Denmark
10 years experience in software design. In my job as a software architect I work with Java and J2EE. .net is so far only a hobby project, but nonetheless interesting Wink | ;-)

Comments and Discussions