Click here to Skip to main content
11,630,646 members (75,581 online)
Click here to Skip to main content
Add your own
alternative version

Dynamic/Transparent Proxy using DynamicProxy.NET

, 21 Oct 2003 177.3K 3.5K 78
Learn how easy it is to create Dynamic/Transparent proxies in .NET using DynamicProxy.NET
dynamicproxy.zip
DynamicProxy
bin
Release
DynamicProxy.dll
docs
tutorial.png
tutorial2.png
DynamicProxy.csproj.user
DynamicProxyTest
App.ico
bin
Release
DynamicProxy.dll
DynamicProxyTest.csproj.user
TestClasses
DynamicProxyTest.exe
dynamicproxytutorial.zip
DynamicProxyTutorial
App.ico
bin
Release
DynamicProxy.dll
DynamicProxyTutorial.exe
DynamicProxyTutorial.csproj.user
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

Share

About the Author

SKI_BUM
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 | ;-)

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150723.1 | Last Updated 22 Oct 2003
Article Copyright 2003 by SKI_BUM
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid