<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Rob Garrett - Blog &#187; Code</title>
	<atom:link href="http://blog.robgarrett.com/tag/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.robgarrett.com</link>
	<description>Software and Technology Tid-bits</description>
	<lastBuildDate>Wed, 17 Feb 2010 01:26:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='blog.robgarrett.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/aab8ef1df0f0164f4c766c98b644e9fb?s=96&#038;d=http://s2.wp.com/i/buttonw-com.png</url>
		<title>Rob Garrett - Blog &#187; Code</title>
		<link>http://blog.robgarrett.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://blog.robgarrett.com/osd.xml" title="Rob Garrett - Blog" />
	<atom:link rel='hub' href='http://blog.robgarrett.com/?pushpress=hub'/>
		<item>
		<title>.NET Wrapper for COM Elevation</title>
		<link>http://blog.robgarrett.com/2007/02/12/net-wrapper-for-com-elevation/</link>
		<comments>http://blog.robgarrett.com/2007/02/12/net-wrapper-for-com-elevation/#comments</comments>
		<pubDate>Mon, 12 Feb 2007 23:03:03 +0000</pubDate>
		<dc:creator>Rob Garrett</dc:creator>
				<category><![CDATA[Everything]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Windows Vista]]></category>

		<guid isPermaLink="false">/cs/blogs/software/archive/2007/02/12/net-wrapper-for-com-elevation.aspx</guid>
		<description><![CDATA[Microsoft has ramped up the security in their latest operating system &#8211; Windows Vista, which means that developers now have to pay more attention to certain security constraints imposed by the operating system when developing applications.&#160;&#160; Those of you readers who have read my prior posts on User Access Control in Vista&#160;(and the further reading [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.robgarrett.com&amp;blog=7688126&amp;post=2473&amp;subd=robgarrett&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Microsoft has ramped up the security in their latest operating system &#8211; Windows Vista, which means that developers now have to pay more attention to certain security constraints imposed by the operating system when developing applications.&nbsp;&nbsp; Those of you readers who have read my prior posts on <a href="http://robgarrett.com/cs/blogs/software/archive/2007/01/04/windows-vista-user-account-control.aspx" target="_blank">User Access Control in Vista</a>&nbsp;(and the <a href="http://robgarrett.com/cs/blogs/software/archive/2007/01/04/windows-vista-uac-further-reading.aspx" target="_blank">further reading</a> article), may be interested in this small software project I put together.&nbsp; </p>
<p><strong>The Problem in a Nutshell</strong></p>
<p>Vista requires that ALL users (even administrators) obtain an elevated security token before being permitted to execute protected API calls&nbsp;or accessing protected system resources (the registry for example).&nbsp; In prior versions of Windows, system administrators were given a privileged security token at login and permitted to perform any task without further confirmation from the interactive user.&nbsp; Windows Vista now allocates a low security token by default to all users and administrators, and an elevated token is only obtained with credentials (users) or confirmation (administrators) when required.</p>
<p>Elevation request is determined by Vista before any process is loaded &#8211; so if a particular process is running non-elevated then access to protected resources and APIs within that process is denied.&nbsp; This is an important caveat, as it affects the way in which&nbsp;software developers&nbsp;approach application design.</p>
<p><strong>How does one Elevate their Process?</strong></p>
<p>There are several articles on the Internet about developing elevated processes in Vista, so I&#8217;ll not dwell on the steps here (my previous articles cover this subject), but essentially&nbsp;elevation request&nbsp;involves decorating .NET assemblies and WIN32 executables with elevation status in the application manifest file (may be embedded or side-by-side).</p>
<p><a href="http://msdn2.microsoft.com/en-us/library/ms679687.aspx" target="_blank">The COM Elevation Moniker</a> is another method for obtaining elevation.&nbsp; Using the CEM it is possible to host a COM (Component Object Model) component in elevated state using&nbsp;the dedicated system process &#8211; DLLHost.exe.&nbsp; <a href="http://www.microsoft.com/downloads/details.aspx?familyid=c2b1e300-f358-4523-b479-f53d234cdccf&amp;displaylang=en" target="_blank">The Windows Vista SDK</a> provides details&nbsp;on adding the necessary code to your&nbsp;project to elevate an out-of-proc&nbsp;component and making it available to your non-elevated application.</p>
<p>So why use the COM Elevation Moniker?</p>
<p>The CEM permits a non-elevated process to execute protected API calls and access protected resources by calling across process boundaries into an elevated process.&nbsp; This is how Vista achieves mid-application elevation &#8211; look for the shield symbols in forms and property pages, to get an idea of what I am talking about.&nbsp; </p>
<p><strong>So what&#8217;s the big deal?</strong></p>
<p>Development of a CEM&nbsp;component typically involves some work in C++.&nbsp; Development of COM components in VB and .NET is of course possible, but when it comes down to accessing WIN32 calls to perform COM elevation most of the examples are published in C++.&nbsp; Developing a managed CEM is possible but involves some work to get it operating correctly.</p>
<p>What about cross boundary communication? &#8211; This is the klutz of the problem with authoring CEM components, how to communicate across process boundaries?&nbsp; COM development answered this problem a long time ago, and the same is true now as it was then &#8211; marshalling data and cross process communication is handled by the COM infrastructure.&nbsp; But what about managed code?&nbsp; If you&#8217;ve managed to solve the problem of invoking a .NET assembly as a COM CEM component, you still have to communicate over .NET interop, which may not be ideal.</p>
<p>Surely someone must have a way to perform elevation in the managed world?</p>
<p><strong>The .NET Wrapper for COM Elevation</strong></p>
<p>I wanted a way for managed developers to attribute their code classes in such away that embedded methods of these classes would request elevation from the Vista operating system before execution of such methods.&nbsp; So I went about developing a library to encapsulate the complexities of the COM Elevation Moniker.&nbsp; The end result is available to download from <a href="http://robgarrett.com/blogs/downloads/elevation.zip">here</a>.</p>
<p>After downloading the ZIP files and unpacking it, the library is packaged as an MSI installer file, which must be installed as an administrator to make use of the library functionality.&nbsp; The installer also installs some example code to demonstrate use of the library.</p>
<p><strong>How does the Managed Wrapper Library Work?</strong></p>
<p>Essentially, the wrapper library exists as a managed and non-managed server.&nbsp; The non-managed COM server provides the infrastructure to elevate managed code by hosting it&#8217;s own .NET AppDomain.&nbsp; The managed&nbsp;server assembly&nbsp;is loaded into the elevated AppDomain and executes code developed by the end user of my library.</p>
<p>Communication between the hosted managed server and client code is achieved using an IPC (Inter-Process Communication) channel developed against WCF (Windows Communication Foundation).&nbsp; The exact specifics of this set up is beyond the scope of this blog post, but involves the use of CodeDOM (dynamic code generation) to create a dynamic messaging system between elevated code and non-elevated client proxy.&nbsp; </p>
<p>My wrapper library is very much &#8220;beta&#8221; at present.&nbsp; I&#8217;ve published my library in the hope that I can attract a number of developers to use it and let me know their initial thoughts.&nbsp; Documentation is sparse, but I am working on publishing a technical manual. It is important to understand that end-users of my library require no knowledge of WCF development, C++, or COM expertise, which makes my library versatile and easy to use by managed developers of different skill-level.</p>
<p>For now, I&#8217;ll end this post with a thank-you &#8211; for taking an interest in my project.&nbsp; I look forward to receiving feedback (good and bad), especially any pertaining to security aspects.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/robgarrett.wordpress.com/2473/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/robgarrett.wordpress.com/2473/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/robgarrett.wordpress.com/2473/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/robgarrett.wordpress.com/2473/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/robgarrett.wordpress.com/2473/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/robgarrett.wordpress.com/2473/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/robgarrett.wordpress.com/2473/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/robgarrett.wordpress.com/2473/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/robgarrett.wordpress.com/2473/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/robgarrett.wordpress.com/2473/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/robgarrett.wordpress.com/2473/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/robgarrett.wordpress.com/2473/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/robgarrett.wordpress.com/2473/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/robgarrett.wordpress.com/2473/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/robgarrett.wordpress.com/2473/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/robgarrett.wordpress.com/2473/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.robgarrett.com&amp;blog=7688126&amp;post=2473&amp;subd=robgarrett&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.robgarrett.com/2007/02/12/net-wrapper-for-com-elevation/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">Rob Garrett</media:title>
		</media:content>
	</item>
		<item>
		<title>Pass by Reference</title>
		<link>http://blog.robgarrett.com/2006/10/25/pass-by-reference/</link>
		<comments>http://blog.robgarrett.com/2006/10/25/pass-by-reference/#comments</comments>
		<pubDate>Wed, 25 Oct 2006 09:17:49 +0000</pubDate>
		<dc:creator>Rob Garrett</dc:creator>
				<category><![CDATA[Everything]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">/cs/blogs/software/archive/2006/10/25/Pass-by-Reference.aspx</guid>
		<description><![CDATA[Contrary to the beliefs of some developers, .Net reference types do not need to be passed as reference parameters (ref in C#, ByRef in VB) when a method is going to alter the contents of an object instance. Passing a object reference by reference parameter just means the method can reassign the reference to a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.robgarrett.com&amp;blog=7688126&amp;post=2301&amp;subd=robgarrett&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Contrary to the beliefs of some developers, .Net reference types do not need to be passed as reference parameters (ref in C#, ByRef in VB) when a method is going to alter the contents of an object instance.
<p />Passing a object reference by reference parameter just means the method can reassign the reference to a new object  instance. The following reference parameter shown  below is unnecessary:
<p />class foo<br />{<br />public int i;<br />}
<p />&#8230; void fooMethod(ref foo fooInst)<br />{<br />fooInst.i = 10;<br />}
<p />.Net is smart enough to pass object instances using address &#8211; not the whole object across the stack. In C++ land, the parameter should be passed as a pointer or explicit reference (using &amp;).</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/robgarrett.wordpress.com/2301/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/robgarrett.wordpress.com/2301/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/robgarrett.wordpress.com/2301/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/robgarrett.wordpress.com/2301/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/robgarrett.wordpress.com/2301/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/robgarrett.wordpress.com/2301/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/robgarrett.wordpress.com/2301/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/robgarrett.wordpress.com/2301/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/robgarrett.wordpress.com/2301/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/robgarrett.wordpress.com/2301/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/robgarrett.wordpress.com/2301/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/robgarrett.wordpress.com/2301/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/robgarrett.wordpress.com/2301/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/robgarrett.wordpress.com/2301/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/robgarrett.wordpress.com/2301/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/robgarrett.wordpress.com/2301/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.robgarrett.com&amp;blog=7688126&amp;post=2301&amp;subd=robgarrett&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.robgarrett.com/2006/10/25/pass-by-reference/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">Rob Garrett</media:title>
		</media:content>
	</item>
		<item>
		<title>Failed to open XML parser COM object.</title>
		<link>http://blog.robgarrett.com/2006/08/15/failed-to-open-xml-parser-com-object/</link>
		<comments>http://blog.robgarrett.com/2006/08/15/failed-to-open-xml-parser-com-object/#comments</comments>
		<pubDate>Tue, 15 Aug 2006 22:46:00 +0000</pubDate>
		<dc:creator>Rob Garrett</dc:creator>
				<category><![CDATA[Everything]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">/cs/blogs/software/archive/2006/08/15/2179.aspx</guid>
		<description><![CDATA[I came across a problem when launching any PPC emulator through Visual Studio&#8217;s Device Emulator Manager &#8211; I&#8217;d receive an error of the sort: Failed to open XML parser COM object. After digging around Google for a while I found the problem was related to a missing installation of MSXML Parser Version 3 (why MS [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.robgarrett.com&amp;blog=7688126&amp;post=2179&amp;subd=robgarrett&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I came across a problem when launching any PPC emulator through Visual Studio&#8217;s Device Emulator Manager &#8211; I&#8217;d receive an error of the sort: <b>Failed to open XML parser COM object</b>. </p>
<p>After digging around Google for a while I found the problem was related to a missing installation of MSXML Parser Version 3 (why MS are not using MSXML6 I do not know).</p>
<p>If you have the DLL on your PC the following command should solve the problem -:</p>
<p><b>regsvr32 %windir%system32msxml3.dll</b></p>
<p>If you&#8217;re missing the DLL the parser can be downloaded from Microsoft.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/robgarrett.wordpress.com/2179/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/robgarrett.wordpress.com/2179/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/robgarrett.wordpress.com/2179/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/robgarrett.wordpress.com/2179/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/robgarrett.wordpress.com/2179/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/robgarrett.wordpress.com/2179/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/robgarrett.wordpress.com/2179/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/robgarrett.wordpress.com/2179/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/robgarrett.wordpress.com/2179/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/robgarrett.wordpress.com/2179/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/robgarrett.wordpress.com/2179/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/robgarrett.wordpress.com/2179/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/robgarrett.wordpress.com/2179/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/robgarrett.wordpress.com/2179/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/robgarrett.wordpress.com/2179/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/robgarrett.wordpress.com/2179/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.robgarrett.com&amp;blog=7688126&amp;post=2179&amp;subd=robgarrett&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.robgarrett.com/2006/08/15/failed-to-open-xml-parser-com-object/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">Rob Garrett</media:title>
		</media:content>
	</item>
		<item>
		<title>C# Value Types and Ranges</title>
		<link>http://blog.robgarrett.com/2006/06/01/c-value-types-and-ranges/</link>
		<comments>http://blog.robgarrett.com/2006/06/01/c-value-types-and-ranges/#comments</comments>
		<pubDate>Fri, 02 Jun 2006 00:59:00 +0000</pubDate>
		<dc:creator>Rob Garrett</dc:creator>
				<category><![CDATA[Everything]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">/cs/blogs/software/archive/2006/06/01/2005.aspx</guid>
		<description><![CDATA[Just because it&#8217;s good to know&#8230;.. Keyword Class Range bool System.Boolean true and false byte System.Byte 0 to 255 sbyte System.SByte -128 to 127 short System.Int16 -32768 to 32767 ushort System.Uint16 0 to 65535 int System.Int32 -2,147,483,648 to 2,147,483,647 uint System.UInt32 0 to 4,294,967,295 long System.Int64 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 ulong System.UInt64 0 to 18,446,744,073,709,551,615 decimal [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.robgarrett.com&amp;blog=7688126&amp;post=2005&amp;subd=robgarrett&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Just because it&#8217;s good to know&#8230;..</p>
<table class="MsoNormalTable" style="border:1pt outset rgb(240,204,0);width:100%;" border="1" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td style="border:1pt inset rgb(240,204,0);width:10%;padding:4.5pt;" width="10%">
<p class="MsoNormal" style="margin-bottom:.0001pt;text-align:center;line-height:normal;" align="center"><b><span style="font-size:12pt;font-family:&quot;color:rgb(0,0,204);">Keyword</span></b><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);width:15%;padding:4.5pt;" width="15%">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><b><span style="font-size:12pt;font-family:&quot;color:rgb(0,102,0);">Class</span></b><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);width:75%;padding:4.5pt;" width="75%">
<p class="MsoNormal" style="margin-bottom:.0001pt;text-align:center;line-height:normal;" align="center"><b><span style="font-size:12pt;font-family:&quot;">Range</span></b><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
</tr>
<tr>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;text-align:center;line-height:normal;" align="center"><span style="font-size:12pt;font-family:&quot;color:rgb(0,0,204);">bool</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;color:rgb(0,102,0);">System.Boolean</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;">true and false </span></p>
</td>
</tr>
<tr>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;text-align:center;line-height:normal;" align="center"><span style="font-size:12pt;font-family:&quot;color:rgb(0,0,204);">byte</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;color:rgb(0,102,0);">System.Byte</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;">0 to 255 </span></p>
</td>
</tr>
<tr>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;text-align:center;line-height:normal;" align="center"><span style="font-size:12pt;font-family:&quot;color:rgb(0,0,204);">sbyte</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;color:rgb(0,102,0);">System.SByte</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;">-128 to 127 </span></p>
</td>
</tr>
<tr>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;text-align:center;line-height:normal;" align="center"><span style="font-size:12pt;font-family:&quot;color:rgb(0,0,204);">short</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;color:rgb(0,102,0);">System.Int16</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;">-32768 to 32767 </span></p>
</td>
</tr>
<tr>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;text-align:center;line-height:normal;" align="center"><span style="font-size:12pt;font-family:&quot;color:rgb(0,0,204);">ushort</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;color:rgb(0,102,0);">System.Uint16</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;">0 to 65535</span></p>
</td>
</tr>
<tr>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;text-align:center;line-height:normal;" align="center"><span style="font-size:12pt;font-family:&quot;color:rgb(0,0,204);">int</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;color:rgb(0,102,0);">System.Int32</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;">-2,147,483,648 to 2,147,483,647</span></p>
</td>
</tr>
<tr>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;text-align:center;line-height:normal;" align="center"><span style="font-size:12pt;font-family:&quot;color:rgb(0,0,204);">uint</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;color:rgb(0,102,0);">System.UInt32</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;">0 to 4,294,967,295</span></p>
</td>
</tr>
<tr>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;text-align:center;line-height:normal;" align="center"><span style="font-size:12pt;font-family:&quot;color:rgb(0,0,204);">long</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;color:rgb(0,102,0);">System.Int64</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;">-9,223,372,036,854,775,808 to<br />
    9,223,372,036,854,775,807</span></p>
</td>
</tr>
<tr>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;text-align:center;line-height:normal;" align="center"><span style="font-size:12pt;font-family:&quot;color:rgb(0,0,204);">ulong</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;color:rgb(0,102,0);">System.UInt64</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;">0 to 18,446,744,073,709,551,615</span></p>
</td>
</tr>
<tr>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;text-align:center;line-height:normal;" align="center"><span style="font-size:12pt;font-family:&quot;color:rgb(0,0,204);">decimal</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;color:rgb(0,102,0);">System.Decimal</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;">-79,228,162,514,264,337,593,543,950,335<br />
    to 79,228,162,514,264,337,593,543,950,335</span></p>
</td>
</tr>
<tr>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;text-align:center;line-height:normal;" align="center"><span style="font-size:12pt;font-family:&quot;color:rgb(0,0,204);">double</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;color:rgb(0,102,0);">System.Double</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;">-1.79769313486232e308 to<br />
    1.79769313486232e308</span></p>
</td>
</tr>
<tr>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;text-align:center;line-height:normal;" align="center"><span style="font-size:12pt;font-family:&quot;color:rgb(0,0,204);">float</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;color:rgb(0,102,0);">System.Single</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;">-3.402823e38 to 3.402823e38</span></p>
</td>
</tr>
<tr>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;text-align:center;line-height:normal;" align="center"><span style="font-size:12pt;font-family:&quot;color:rgb(0,0,204);">char</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;color:rgb(0,102,0);">System.Char</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;">0 to 65535</span></p>
</td>
</tr>
</tbody>
</table>
<p></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/robgarrett.wordpress.com/2005/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/robgarrett.wordpress.com/2005/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/robgarrett.wordpress.com/2005/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/robgarrett.wordpress.com/2005/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/robgarrett.wordpress.com/2005/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/robgarrett.wordpress.com/2005/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/robgarrett.wordpress.com/2005/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/robgarrett.wordpress.com/2005/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/robgarrett.wordpress.com/2005/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/robgarrett.wordpress.com/2005/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/robgarrett.wordpress.com/2005/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/robgarrett.wordpress.com/2005/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/robgarrett.wordpress.com/2005/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/robgarrett.wordpress.com/2005/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/robgarrett.wordpress.com/2005/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/robgarrett.wordpress.com/2005/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.robgarrett.com&amp;blog=7688126&amp;post=2005&amp;subd=robgarrett&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.robgarrett.com/2006/06/01/c-value-types-and-ranges/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">Rob Garrett</media:title>
		</media:content>
	</item>
		<item>
		<title>Code Access Security – A Primer</title>
		<link>http://blog.robgarrett.com/2006/06/01/code-access-security-%e2%80%93-a-primer/</link>
		<comments>http://blog.robgarrett.com/2006/06/01/code-access-security-%e2%80%93-a-primer/#comments</comments>
		<pubDate>Thu, 01 Jun 2006 05:37:00 +0000</pubDate>
		<dc:creator>Rob Garrett</dc:creator>
				<category><![CDATA[Everything]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">/cs/blogs/software/archive/2006/05/31/1994.aspx</guid>
		<description><![CDATA[Overview This post serves as a primer for software developers interested in learning about Code Access Security (CAS) in .NET. The following information is not exhaustive of the subject matter and contains the basic overview of Code-Access-Security. Those interested in this subject are encouraged to read further. The following articles cover code security and are [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.robgarrett.com&amp;blog=7688126&amp;post=1994&amp;subd=robgarrett&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div>
<h1>Overview<br />
</h1>
</div>
<p>This post serves as a primer for software developers interested in learning about Code Access Security (CAS) in .NET.  The following information is not exhaustive of the subject matter and contains the basic overview of Code-Access-Security.  Those interested in this subject are encouraged to read further.
</p>
<p>The following articles cover code security and are a good follow-up to this post.
</p>
<p><a href="http://www.codeproject.com/dotnet/UB_CAS_NET.asp">http://www.codeproject.com/dotnet/UB_CAS_NET.asp<br />
</a></p>
<p><a href="http://msdn.microsoft.com/msdnmag/issues/05/11/CodeAccessSecurity/default.aspx">http://msdn.microsoft.com/msdnmag/issues/05/11/CodeAccessSecurity/default.aspx</a><span style="font-size:10px;"><br />
		</span></p>
<p><a href="http://msdn.microsoft.com/msdnmag/issues/05/11/HostingAddIns/">http://msdn.microsoft.com/msdnmag/issues/05/11/HostingAddIns/</a>
	</p>
<p>Shawn Farkas is one of many experts on Code Access Security, and as well as the author of many magazines, he posts regularly on his weblog:
</p>
<p><a href="http://blogs.msdn.com/shawnfa/">http://blogs.msdn.com/shawnfa/</a><span style="font-size:10px;"><br />
		</span></p>
<p>
&nbsp;</p>
<div>
<h1>What is Code Access Security?<br />
</h1>
</div>
<p>Most computer users and security experts are accustomed to Role-Based Security (RBS), where particular users belong to specific groups, assigned permissions to protected resources.  Windows XP/2003, SQL Server, IIS, and a host of server applications use Role-Based Security to provide access protection.
</p>
<p>Code Access Security is different to Role-Based Security in that it restricts access to protected resources at the code level.  Coming from a role-based way of thinking, code access security can be a confusing concept because there is no user attempting access in the typical sense.  Code Access Security defines a set of permissions and the policy, which defines assignment of those permissions, by evaluating the evidence belonging to the code requesting access.
</p>
<p>
&nbsp;</p>
<div>
<h1>Why should we care about Code Access Security?<br />
</h1>
</div>
<p>Typically, software development and security roles are very distinctive:
</p>
<p><em>Software developers create software to run on workstations and servers, and security experts lock down access at the user level to these workstations and servers</em>.
</p>
<p>The above approach has been in place for as long as developers have been creating software and the software has been manipulating secured data; however, this methodology has a few flaws:
</p>
<ul>
<li>
<div>Deployment of software in the above scheme is troublesome – developers are used to writing and testing software with a full set of permissions.  When deploying software, developed in this fashion, in a locked-down environment, the software often fails.
</div>
</li>
<li>
<div>The best software developers are not always the best security experts, and vice versa.  Software developers hate to work through security constraints and security experts often like to lock down systems to the point where they are sometimes unusable.
</div>
</li>
</ul>
<p>Code Access Security is a new way of thinking.  Just as industry has learned that performance is not a last minute consideration in the software development lifecycle, neither is security.  Code Access Security prevents malicious code penetrating secure systems by detecting insecure code before it executes, and potential security holes be pinpointed to code modules that demand a higher permission set.</p>
<p>With Code Access Security, you can:
</p>
<ul>
<li>
<div>Restrict what code can do
</div>
</li>
<li>
<div>Restrict who can call code
</div>
</li>
<li>
<div>Identify code
</div>
</li>
</ul>
<p>Code Access Security works hand-in-hand with security design and threat modeling, in that any .NET assembly can be marked as “security transparent.”  Security transparent assemblies contain code that does not access protected resources, and is safe to operate in partial trust environments.  More on security transport assemblies later in this  post.
</p>
<p>Some environments, in which custom code may execute, are partial trust.  Microsoft guidelines suggest that all ASP.NET installations hosting multiple applications be set at medium trust to guarantee autonomy.  Developers writing code for hosted environments will have no choice but to make sure their code runs at ASP.NET medium trust level.  The next version of SharePoint (Office 12 Server and WSS 3.0) operates at partial trust out of the box.
</p>
<p>
&nbsp;</p>
<div>
<h1>The Fundamentals<br />
</h1>
</div>
<p>As mentioned in the previous section, Code Access Security does not use user or role identification, so how does Code Access Security in .NET work?
</p>
<p>Before execution of verifiable code, the .NET platform determines if the code has permission to complete its function successfully.  This process involves collecting information about the code – <strong>evidence</strong>, determining the required <strong>permissions</strong> to complete execution by obtaining the current <strong>policy</strong> for the enterprise/machine/user/app domain.  The list below further documents the main constituents to Code Access Security:
</p>
<ul>
<li>
<div><strong>Evidence</strong> is a set of attributes that belong to code.  For example, certain .NET assemblies may be strong named and have a particular public key token.  Other assemblies may have originated via “Click Once Deployment” at a certain web address, or reside within a particular directory on the file system.</p>
</div>
</li>
<li>
<div><strong>Permissions</strong> represent access to a protected resource or the ability to perform a protected operation.  The .NET Framework provides a number of classes that represent different permissions.  For example, if some code needs access to files on disk then a <span style="font-family:Courier New;font-size:10px;">FileIOPermission</span> is required; the <span style="font-family:Courier New;font-size:10px;">ReflectionPermission </span>is required for any code that attempts to perform refection, etc.</p>
</div>
</li>
<li>
<div><strong>Permission Set</strong> is a collection of permissions.  The system defines several permission sets and different assemblies in a .NET application may fall into zero, one or more of these permission sets.  The Framework defines a number of default permission sets, including “<em>Full Trust”</em> – a set that contains all permissions, and “<em>Nothing”</em> – a set that contains no permissions.</p>
</div>
</li>
<li>
<div><strong>Code Group</strong> is mapping of evidence to permission sets.  Code groups combine to form a tree where code must exhibit the desired evidence to satisfy membership of the group.
</div>
</li>
<li>
<div><strong>Security Policy</strong> is a configurable set of rules that the CLR follows when determining the permissions to grant to code.  There exist four independent policy levels:</p>
</div>
</li>
</ul>
<ul>
<li>
<div>Enterprise – All managed code in an enterprise setting
</div>
</li>
<li>
<div>Machine – All managed code on a single computer
</div>
</li>
<li>
<div>User – Managed code in all processes associated with the current user
</div>
</li>
<li>
<div>Application Domain – Managed code in the host’s application domain</div>
</li>
</ul>
<div>
<div>
<h1>What about ASP.NET?<br />
</h1>
</div>
</div>
<p>ASP.NET builds atop of Code Access Security and provides five permission sets; each set depicted as a trust level:
</p>
<ul>
<li>
<div>Full
</div>
</li>
<li>
<div>High
</div>
</li>
<li>
<div>Medium
</div>
</li>
<li>
<div>Low
</div>
</li>
<li>
<div>Minimal
</div>
</li>
</ul>
<p>Each trust level above contains permissions, ranging from a complete set of permissions &#8211; “Full” trust &#8211; to limited permissions &#8211; “Minimal” trust.
</p>
<p>A separate policy configuration file exists for each trust level and packaged with the ASP.NET installation.  An ASP.NET application stipulates the level of trust and location of policy file in the application configuration file (web.config):
</p>
<p><span style="font-family:Courier New;font-size:10px;">&lt;trustLevel name=&#8221;High&#8221; policyFile=&#8221;web_hightrust.config&#8221;/&gt;<br />
</span></p>
<p>Applications that operate in partial trust (not full trust) and require elevated permissions can run in a higher trust level or by defining custom permissions in a new policy file.  If an application only requires a handful of permissions, not present at the current trust level, then it makes sense to define a custom policy and permission set.  Increasing the trust level may add many more permissions not required by the application, creating security vulnerability.
</p>
<p>
&nbsp;</p>
<div>
<h1>Applying Code Access Security<br />
</h1>
</div>
<p>Two different kinds of syntax are available when adding Code Access Security to code: declarative and imperative.
</p>
<p>Declarative syntax involves applying attributes to methods, classes, or assemblies.  The “Just-in-Time” (JIT) compiler reads meta-data generated from these attributes to evaluate these calls.
</p>
<p><span style="font-family:Courier New;font-size:10px;">
</p>
<p></span>[FileIOPermission(SecurityAction.Demand, Unrestricted=true)]<br />
<br />
public class Foo { &#8230; }<br />
</p>
<p>Imperative syntax involves the use of method calls to create instances of security classes at runtime.
</p>
<p><span style="font-family:Courier New;font-size:10px;">
</p>
<p></span>public class Foo<br />
<br />
{<br />
<br />
public void MethodOne(..)<br />
<br />
{<br />
<br />
    new FileIOPermission(PermissionState.Unrestricted).Demand();<br />
<br />
}<br />
<br />
}<br />
</p>
<p>Both of the examples above are requesting unrestricted access to the file system.  Most of the security permission classes in the .NET framework provide properties to customize the level of access; the <span style="font-family:Courier New;font-size:10px;">FileIOPermission</span> includes properties to permit read/write access to particular files and directories in the file system.  The example below permits all access to a particular file by changing the parameters passed to the constructor:
</p>
<p><span style="font-family:Courier New;font-size:10px;">new FileIOPermission(FileIOPermissionAccess.AllAccess,&#8221;C:\Test.txt&#8221;).Demand();<br />
</span></p>
<p>So, what happens when code declares a security permission attribute or instantiates a new permission class imperatively?
</p>
<p>All three examples above call a “demand” on the desired permission class.  The demand instructs the CLR to walk the call stack of the current process making sure that each method call has the desired permission requested.  If one of the calling methods in the stack does not have the permission then the CLR throws a security exception.
</p>
<p>Most of the classes in the .NET Framework demand (or link demand) permissions when accessing protected resources.  If a developer writes code that uses one of the framework classes, say to access a database or perform reflection, and the developer’s code is running in partial trust, then the developer’s code must the desired permission, otherwise the CLR will throw a security exception.
</p>
<p>By default, any code developed against the .NET framework runs as “full trust.” except in the following cases:
</p>
<ul>
<li>
<div>The developer explicitly creates a sandbox application domain with partial trust
</div>
</li>
<li>
<div>Configures application assemblies as partial trust using the .NET Framework Configuration tool
</div>
</li>
<li>
<div>Runs the application code in ASP.NET at a trust level other than full
</div>
</li>
<li>
<div>Is running the code in some other host application preconfigured to partial trust
</div>
</li>
<li>
<div>The code is executed across a network
</div>
</li>
</ul>
<p>When operating at “full trust” all security demands made by classes in the framework (or by custom developer classes that are security aware) succeed.  Only during deployment to a partial trust environment is there a problem.  Developers should get in the habit of developing under partial trust when developing code that access protected resources.
</p>
<p>Permission demand is one of several actions that applicable to permission classes, other actions available are:
</p>
<ul>
<li>
<div><span style="font-family:Courier New;font-size:10px;">SecurityAction.Demand</span> – All callers higher in the call stack must have the permission specified by the current permission object.</p>
</div>
</li>
<li>
<div><span style="font-family:Courier New;font-size:10px;">SecurityAction.LinkDemand</span> –Only the immediate caller in the call stack must have the permission specified by the current permission object.</p>
</div>
</li>
<li>
<div><span style="font-family:Courier New;font-size:10px;">SecurityAction.InheritanceDemand</span> &#8211; Derived classes or overriding methods must have the permission specified by the current permission object.</p>
</div>
</li>
<li>
<div><span style="font-family:Courier New;font-size:10px;">SecurityAction.Assert</span> – If the calling code has the desired permission then the stack walk for permission check stops.  Use asserts only when encapsulating code that is known to be secure because callers further up the stack running in partial trust may not be aware of a demand further down the chain.  Code containing asserts without the actual permission will allow permission checking to continue up the call stack.</p>
</div>
</li>
<li>
<div><span style="font-family:Courier New;font-size:10px;">SecurityAction.Deny</span> – Callers cannot access a protected resource specified by the permission, even if the caller has permission to access the resource.  So if a method in the call stack specifies a deny action and a method further down the chain attempts to access the resource, regardless of whether they have the permission, the method lower in the call stack will fail access.</p>
</div>
</li>
<li>
<div><span style="font-family:Courier New;font-size:10px;">SecurityAction.PermitOnly</span> &#8211; Link a deny action only a permit only action specifies that the caller is denied access to all resources except for those defined in the current permission object.  Further definition of this action is beyond the scope of this post.</p>
</div>
</li>
<li>
<div><span style="font-family:Courier New;font-size:10px;">SecurityAction.RequestMinimum</span> &#8211; Only used within the scope of an assembly, this action defines the set of minimum permissions required for the assembly to execute.</p>
</div>
</li>
<li>
<div><span style="font-family:Courier New;font-size:10px;">SecurityAction.RequestOptional</span> – Only used within the scope of an assembly, this action defines the set of permissions optional to execute (not required).</p>
</div>
</li>
<li>
<div><span style="font-family:Courier New;font-size:10px;">SecurityAction.RequestRefuse</span> &#8211; Only used within the scope of an assembly, this action defines a set of permissions that may be requested and misused, and should therefore never be granted, even if the current security policy allows it.  Further definition of this action is beyond the scope of this post.
</div>
</li>
</ul>
<p>Asserts deserve special consideration because they prevent permission demands from reaching callers higher in the call stack.  Asserts are useful when a method is required to call some code that demands higher permission and the caller of the method is in partial trust.  For example, a trusted custom assembly with elevated trust could call out to the file system using one of the framework API calls; the framework will demand a <span style="font-family:Courier New;font-size:10px;">FileIOPermission</span>, which must not propagate beyond the level of the custom assembly.  Placing assert code around the call to the file system API will ensure that that demand never leaves the scope of the method containing the assert code.  The custom assembly must have the <span style="font-family:Courier New;font-size:10px;">FileIOPermission</span>, otherwise the assert code is ignored and demands will continue up the stack to partial trusted code.  The following is an example of an assertion code around a call to a method, which demands security permission.  Notice the revert call at the end of the code, this revert will cancel the assert code.  It is important to limit the scope of assertion so to avoid creating a security vulnerability, place only the code that requires the security permission between the assert call and the revert call.
</p>
<p><span style="font-family:Courier New;font-size:10px;"></span></p>
<p>new FileIOPermission(PermissionState.Unrestricted).Assert();<br />
<br />
// Do something that causes a FileIOPermission<br />
<br />
CodeAccessPermission.RevertAssert();<br />
</p>
<p>
&nbsp;</p>
<div>
<h1>Transparent Assemblies<br />
</h1>
</div>
<p>Transparent assemblies are .NET assemblies that are free from security critical code.  The .NET Framework 2.0 enables developers to define assemblies as transparent so that security audits can rule out these assemblies as potentially security vulnerable.  Transparent assemblies voluntarily give up the ability to elevate the permissions of the call stack, and the following rules apply:
</p>
<ul>
<li>
<div>Transparent code cannot asset for permissions to stop a stack walk from continuing
</div>
</li>
<li>
<div>Transparent code cannot satisfy a link demand
</div>
</li>
<li>
<div>Unverifiable code is forbidden in transparent assemblies
</div>
</li>
<li>
<div>Calls to P/Invoke or unmanaged code will cause a security permission demand
</div>
</li>
</ul>
<p>Security transparent assemblies run either at the permission level granted, or at the permission level of the caller, whichever is less.
</p>
<p>By default, all assemblies are security critical – the opposite of security transparent – but made into a transparent assembly by adding the following attribute at the assembly level:
</p>
<p><span style="font-family:Courier New;font-size:10px;">[assembly:SecurityTransparent]<br />
</span></p>
<p>The CLR throws a security exception if a transparent assembly attempts to elevate permissions.  In cases where the developer wants to make the entire assembly as transparent, except for a few methods, use the following attribute:
</p>
<p><span style="font-family:Courier New;font-size:10px;">[assembly:SecurityCritical]<br />
</span></p>
<p>The attribute named above is a little misleading in that it marks the entire assembly as transparent but allows security critical code.  Decorate methods that require elevated as follows:
</p>
<p><span style="font-family:Courier New;font-size:10px;">
</p>
<p></span>[SecurityCritical]<br />
<br />
public void foo()<br />
<br />
{<br />
<br />
new FileIOPermission(PermissionState.Unrestricted).Demand();<br />
<br />
…..<br />
<br />
}<br />
</p>
<p>
&nbsp;</p>
<div>
<h1>Allowing Partially Trusted Callers<br />
</h1>
</div>
<p>By default, strongly named, trusted assemblies obtain an implicit link demand for full trust on every public method of every publicly available class within the assembly.  The CLR performs this insertion to protect fully trusted assemblies from misused by attackers.  For example, a trusted assembly may have full access to loading a disk file.  An attacker realizes that the assembly has not been security audited, and can manipulate the file loaded.  The implicit link demand ensures that the attacker cannot execute the method if not running in full trust.
</p>
<p>Assuming developers have security audited their code and want to allow partially trusted callers to call a full trusted assembly – the “Allow Partially Trusted Callers Attribute” (APTCA) enables developers to suppress the implicit link demand:
</p>
<p><span style="font-family:Courier New;font-size:10px;">[assembly: AllowPartiallyTrustedCallers]<br />
</span></p>
<p><strong>Developers should take the utmost care when enabling partially trusted callers to call trusted assemblies.<br />
</strong></p>
<p>Some APTCA assemblies may still demand or link demand explicit permissions, in which case the addition of the APTCA does not remove the explicit demands, and a security exception generated in partially trusted code. </p>
<p></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/robgarrett.wordpress.com/1994/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/robgarrett.wordpress.com/1994/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/robgarrett.wordpress.com/1994/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/robgarrett.wordpress.com/1994/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/robgarrett.wordpress.com/1994/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/robgarrett.wordpress.com/1994/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/robgarrett.wordpress.com/1994/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/robgarrett.wordpress.com/1994/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/robgarrett.wordpress.com/1994/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/robgarrett.wordpress.com/1994/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/robgarrett.wordpress.com/1994/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/robgarrett.wordpress.com/1994/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/robgarrett.wordpress.com/1994/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/robgarrett.wordpress.com/1994/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/robgarrett.wordpress.com/1994/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/robgarrett.wordpress.com/1994/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.robgarrett.com&amp;blog=7688126&amp;post=1994&amp;subd=robgarrett&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.robgarrett.com/2006/06/01/code-access-security-%e2%80%93-a-primer/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">Rob Garrett</media:title>
		</media:content>
	</item>
		<item>
		<title>Right-Click ListBox &#8211; Select Item</title>
		<link>http://blog.robgarrett.com/2006/04/05/right-click-listbox-select-item/</link>
		<comments>http://blog.robgarrett.com/2006/04/05/right-click-listbox-select-item/#comments</comments>
		<pubDate>Thu, 06 Apr 2006 00:20:00 +0000</pubDate>
		<dc:creator>Rob Garrett</dc:creator>
				<category><![CDATA[Everything]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">/cs/blogs/software/archive/2006/04/05/1915.aspx</guid>
		<description><![CDATA[Useful to know &#8211; to select items in a ListBox with a right mouse click wire up the following code to the MouseDown event: private void LB_MouseDown(object sender, MouseEventArgs e) { // Select item if right click. if (MouseButtons.Right == e.Button) { Point pt = new Point(e.X, e.Y); LB.SelectedIndex = LB.IndexFromPoint(pt); } }<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.robgarrett.com&amp;blog=7688126&amp;post=1915&amp;subd=robgarrett&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Useful to know &#8211; to select items in a ListBox with a right mouse click wire up the following code to the MouseDown event:</p>
<p>private void LB_MouseDown(object sender, MouseEventArgs e)<br />
{<br />
  // Select item if right click.<br />
  if (MouseButtons.Right == e.Button)<br />
  {<br />
    Point pt = new Point(e.X, e.Y);<br />
    LB.SelectedIndex = LB.IndexFromPoint(pt);<br />
  }<br />
}</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/robgarrett.wordpress.com/1915/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/robgarrett.wordpress.com/1915/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/robgarrett.wordpress.com/1915/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/robgarrett.wordpress.com/1915/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/robgarrett.wordpress.com/1915/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/robgarrett.wordpress.com/1915/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/robgarrett.wordpress.com/1915/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/robgarrett.wordpress.com/1915/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/robgarrett.wordpress.com/1915/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/robgarrett.wordpress.com/1915/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/robgarrett.wordpress.com/1915/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/robgarrett.wordpress.com/1915/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/robgarrett.wordpress.com/1915/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/robgarrett.wordpress.com/1915/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/robgarrett.wordpress.com/1915/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/robgarrett.wordpress.com/1915/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.robgarrett.com&amp;blog=7688126&amp;post=1915&amp;subd=robgarrett&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.robgarrett.com/2006/04/05/right-click-listbox-select-item/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">Rob Garrett</media:title>
		</media:content>
	</item>
		<item>
		<title>Code Snippets</title>
		<link>http://blog.robgarrett.com/2006/03/24/code-snippets/</link>
		<comments>http://blog.robgarrett.com/2006/03/24/code-snippets/#comments</comments>
		<pubDate>Fri, 24 Mar 2006 21:49:00 +0000</pubDate>
		<dc:creator>Rob Garrett</dc:creator>
				<category><![CDATA[Everything]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">/cs/blogs/software/archive/2006/03/24/1903.aspx</guid>
		<description><![CDATA[Like the code snippets in Visual Studio 2005? Want some more?&#160; Check out http://msdn.microsoft.com/vstudio/downloads/codesnippets/default.aspx<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.robgarrett.com&amp;blog=7688126&amp;post=1903&amp;subd=robgarrett&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Like the code snippets in Visual Studio 2005? Want some more?&nbsp; <br />Check out <a href="http://msdn.microsoft.com/vstudio/downloads/codesnippets/default.aspx">http://msdn.microsoft.com/vstudio/downloads/codesnippets/default.aspx</a></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/robgarrett.wordpress.com/1903/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/robgarrett.wordpress.com/1903/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/robgarrett.wordpress.com/1903/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/robgarrett.wordpress.com/1903/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/robgarrett.wordpress.com/1903/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/robgarrett.wordpress.com/1903/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/robgarrett.wordpress.com/1903/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/robgarrett.wordpress.com/1903/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/robgarrett.wordpress.com/1903/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/robgarrett.wordpress.com/1903/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/robgarrett.wordpress.com/1903/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/robgarrett.wordpress.com/1903/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/robgarrett.wordpress.com/1903/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/robgarrett.wordpress.com/1903/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/robgarrett.wordpress.com/1903/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/robgarrett.wordpress.com/1903/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.robgarrett.com&amp;blog=7688126&amp;post=1903&amp;subd=robgarrett&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.robgarrett.com/2006/03/24/code-snippets/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">Rob Garrett</media:title>
		</media:content>
	</item>
		<item>
		<title>Access modifiers on property get/set</title>
		<link>http://blog.robgarrett.com/2006/02/25/access-modifiers-on-property-getset/</link>
		<comments>http://blog.robgarrett.com/2006/02/25/access-modifiers-on-property-getset/#comments</comments>
		<pubDate>Sun, 26 Feb 2006 01:41:00 +0000</pubDate>
		<dc:creator>Rob Garrett</dc:creator>
				<category><![CDATA[Everything]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">/cs/blogs/software/archive/2006/02/25/1866.aspx</guid>
		<description><![CDATA[To some I may be stating the obvious, but today I was happy to find out that C# lets you set a different access level on the get and set for a property. The example below will help to illustrate what I&#8217;m talking about: public DateTime UpdateDate { get { object data = ViewState["UpdateDate"]; return [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.robgarrett.com&amp;blog=7688126&amp;post=1866&amp;subd=robgarrett&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>To some I may be stating the obvious, but today I was happy to find out that C# lets you set a different access level on the get and set for a property. The example below will help to illustrate what I&#8217;m talking about:</p>
<p>public DateTime UpdateDate<br />
{<br />
    get<br />
    {<br />
        object data = ViewState["UpdateDate"];<br />
        return (null == data ? DateTime.Now : (DateTime)data);<br />
    }<br />
    private set<br />
    {<br />
        ViewState["UpdateDate"] = value;<br />
    }<br />
}<br />
<br />
Why on Earth would you want to do this? Well, I found a perfect situation where I needed the above code. I wanted to expose a property that is essentially read only to any callers that use it, but writable to the encapsulating class. In the above case, I do not use a private field to store the property value, but the ViewState instead (property is part of a server control class), the private set allows the class to write a value to the ViewState for the property.&nbsp; Now, I could have written to the ViewState directly from my class and done away with the property set altogether, but this would have resulted in many statements throughout my class code.&nbsp; This way, if I want to change the logic in how my property value is persisted I can make the necessary changes in one spot.&nbsp; The alternative is to use a private set function, but a private property set is so much nicer. :)</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/robgarrett.wordpress.com/1866/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/robgarrett.wordpress.com/1866/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/robgarrett.wordpress.com/1866/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/robgarrett.wordpress.com/1866/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/robgarrett.wordpress.com/1866/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/robgarrett.wordpress.com/1866/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/robgarrett.wordpress.com/1866/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/robgarrett.wordpress.com/1866/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/robgarrett.wordpress.com/1866/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/robgarrett.wordpress.com/1866/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/robgarrett.wordpress.com/1866/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/robgarrett.wordpress.com/1866/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/robgarrett.wordpress.com/1866/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/robgarrett.wordpress.com/1866/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/robgarrett.wordpress.com/1866/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/robgarrett.wordpress.com/1866/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.robgarrett.com&amp;blog=7688126&amp;post=1866&amp;subd=robgarrett&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.robgarrett.com/2006/02/25/access-modifiers-on-property-getset/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">Rob Garrett</media:title>
		</media:content>
	</item>
		<item>
		<title>HTTP SOAP Remoting Problem</title>
		<link>http://blog.robgarrett.com/2006/02/23/http-soap-remoting-problem/</link>
		<comments>http://blog.robgarrett.com/2006/02/23/http-soap-remoting-problem/#comments</comments>
		<pubDate>Fri, 24 Feb 2006 00:02:00 +0000</pubDate>
		<dc:creator>Rob Garrett</dc:creator>
				<category><![CDATA[Everything]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">/cs/blogs/software/archive/2006/02/23/1858.aspx</guid>
		<description><![CDATA[I am developing a simple .NET remoting server using an HTTP channel and SOAP formatter, and ran into a problem earlier with invalid SOAP actions.&#160; When invoking a method on a remoted object I was getting the following error message: Unhandled Exception: System.Runtime.Remoting.RemotingException: &#160;Invalid SOAPAction specified: blah blah blah After trawling the web for an [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.robgarrett.com&amp;blog=7688126&amp;post=1858&amp;subd=robgarrett&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I am developing a simple .NET remoting server using an HTTP channel and SOAP formatter, and ran into a problem earlier with invalid SOAP actions.&nbsp; When invoking a method on a remoted object I was getting the following error message:</p>
<p><span style="font-weight:bold;">Unhandled Exception: System.Runtime.Remoting.RemotingException:  </span><br /><span style="font-weight:bold;">&nbsp;Invalid SOAPAction specified:</span> blah blah blah</p>
<p>After trawling the web for an hour I established that many other developers are running into the same problem.&nbsp; Now that I&#8217;ve resolved the issue I wanted to share the solution with everyone else.</p>
<p>My simple remoting server is a MarshalByRef class, called <span style="font-style:italic;">MyAgent</span>,<br />
hosted in a Windows service.&nbsp; The HTTP channel, SOAP formatter, and<br />
exposed remoted class are configured in the service application<br />
configuration file as follows.</p>
<p>&lt;system.runtime.remoting&gt;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;application name=&#8221;RPCServer&#8221;&gt;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;service&gt;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;wellknown mode=&#8221;SingleCall&#8221; type=&#8221;MyNamespace.MyAgent, MyAgentLib&#8221; objectUri=&#8221;MyAgent&#8221; /&gt;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/service&gt;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;channels&gt;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;channel port=&#8221;8050&#8243; ref=&#8221;http&#8221;&gt;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;serverProviders&gt;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;formatter ref=&#8221;soap&#8221;/&gt;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/serverProviders&gt;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/channel&gt;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/channels&gt;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/application&gt;<br />&nbsp;&nbsp;&nbsp; &lt;/system.runtime.remoting&gt;</p>
<p>In my client application, I configure the HTTP channel, SOAP formatter, and remote service class as follows:</p>
<p>&lt;system.runtime.remoting&gt;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;application name=&#8221;RPCClient&#8221;&gt;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;client url=&#8221;http://localhost:8050/RPCServer&#8221;&gt;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;wellknown type=&#8221;MyNamespace.MyAgent, MyAgentLib&#8221; url=&#8221;MyAgent&#8221;/&gt;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/client&gt;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;channels&gt;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;channel ref=&#8221;http&#8221; port=&#8221;0&#8243;&gt;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;clientProviders&gt;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;formatter ref=&#8221;soap&#8221;/&gt;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/clientProviders&gt;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/channel&gt;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/channels&gt;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/application&gt;<br />&nbsp;&nbsp;&nbsp; &lt;/system.runtime.remoting&gt;</p>
<p>Originally, the <span style="font-style:italic;">MyAgent </span>class<br />
was a shared C# code file, compiled into both client and server<br />
projects (in remoting, both client and server require the remoted class<br />
type) &#8211; this worked fine in another remoting project I wrote, which<br />
used IPC and TCP channels.&nbsp; When using the HTTP channel and SOAP<br />
provider this approach does not work (using HTTP and binary formatter<br />
gave me a different cryptic error).&nbsp; For some reason, the remoting<br />
proxy (generated by the .NET remoting infrastructure) gets confused and<br />
sees the client version of <span style="font-style:italic;">MyAgent</span> class to be different to that of the <span style="font-style:italic;">MyAgent</span> class hosted in the remote server.&nbsp; To fix the problem I created a shared class library, called <span style="font-style:italic;">MyAgentLib</span>, and included the <span style="font-style:italic;">MyAgent</span> class. I then pointed both client and server projects to the shared class library.&nbsp;&nbsp;&nbsp;&nbsp; Tada!</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/robgarrett.wordpress.com/1858/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/robgarrett.wordpress.com/1858/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/robgarrett.wordpress.com/1858/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/robgarrett.wordpress.com/1858/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/robgarrett.wordpress.com/1858/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/robgarrett.wordpress.com/1858/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/robgarrett.wordpress.com/1858/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/robgarrett.wordpress.com/1858/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/robgarrett.wordpress.com/1858/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/robgarrett.wordpress.com/1858/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/robgarrett.wordpress.com/1858/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/robgarrett.wordpress.com/1858/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/robgarrett.wordpress.com/1858/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/robgarrett.wordpress.com/1858/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/robgarrett.wordpress.com/1858/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/robgarrett.wordpress.com/1858/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.robgarrett.com&amp;blog=7688126&amp;post=1858&amp;subd=robgarrett&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.robgarrett.com/2006/02/23/http-soap-remoting-problem/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">Rob Garrett</media:title>
		</media:content>
	</item>
		<item>
		<title>NOAA Weather Service and .NET</title>
		<link>http://blog.robgarrett.com/2006/01/24/noaa-weather-service-and-net/</link>
		<comments>http://blog.robgarrett.com/2006/01/24/noaa-weather-service-and-net/#comments</comments>
		<pubDate>Wed, 25 Jan 2006 02:24:00 +0000</pubDate>
		<dc:creator>Rob Garrett</dc:creator>
				<category><![CDATA[Everything]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Web Services]]></category>

		<guid isPermaLink="false">/cs/blogs/software/archive/2006/01/24/1822.aspx</guid>
		<description><![CDATA[I have been fighting with the national weather web service, provided by the National Oceanic and Atmospheric Administration, today.&#160; Like a lot of developers, who like to make their web sites fancy with the current weather forecast, I have been developing a component for the web site I am working on. NOAA seems to be [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.robgarrett.com&amp;blog=7688126&amp;post=1822&amp;subd=robgarrett&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I have been fighting with the <a href="http://www.weather.gov/xml/">national weather web service, provided by the National Oceanic and Atmospheric Administration</a>, today.&nbsp; Like a lot of developers, who like to make their web sites fancy with the current weather forecast, I have been developing a component for the web site I am working on. </p>
<p>NOAA seems to be the norm for providing weather data in XML via the <acronym title="Simple Object Access Protocol">SOAP</acronym> web service protocol.&nbsp; Up until a couple of weeks ago, the C# code that I had to connect to the NOAA web service and pull weather data, was working&nbsp; nicely&#8230;.then it stopped &#8211; bah.&nbsp; The proxy code (generated from the service <acronym title="Web Services Description Language">WSDL</acronym> by .NET) kept throwing an HTTP protocol violation exception:
<pre>Unhandled Exception: System.Net.WebException: Error getting responsestream (Rea dDone2): ReceiveFailure ---&gt; System.Exception:         at System.Net.WebConnection.HandleError ()         at System.Net.WebConnection.ReadDone ()         at System.MulticastDelegate.invoke_void_IAsyncResult ()         at System.Net.Sockets.Socket+SocketAsyncResult.Complete ()         at System.Net.Sockets.Socket+Worker.Receive ()         at System.MulticastDelegate.invoke_void ()in &lt;0x000ad&gt; System.Net.WebConnection:HandleError (WebExceptionStatusst, System.Exception e, System.String where)--- End of inner exceptionstack trace ---in &lt;0x00158&gt; System.Net.HttpWebRequest:EndGetResponse (IAsyncResultasyncResult)in &lt;0x00047&gt; System.Net.HttpWebRequest:GetResponse ()in &lt;0x0004d&gt;System.Web.Services.Protocols.WebClientProtocol:GetWebResponse (System.Net.WebRequest request)</pre>
<p>It seems that NOAA changed something involving their service protocol, because the response to a SOAP request is now being returned in separate <a href="http://www.jmarshall.com/easy/http/#http1.1c2">chunked HTTP responses</a>, which causes the .NET SOAP client to puke.</p>
<p>After some digging on the Internet (lots of digging in fact), I came across a <a href="http://lists.ximian.com/pipermail/mono-list/2005-October/028929.html">post</a> on a Mono newsgroup, which gave me a work around to the problem.&nbsp; The trick is to change the web request in the SOAP proxy to use HTTP version 1.0 instead of 1.1.&nbsp; Since HTTP 1.0 does not support chunked response, the NOAA server sends the response data as one block, which keeps the .NET SOAP client happy.&nbsp; </p>
<p>Below is the code for my web service proxy, drop it into your project (assuming you use C#) and instantiate the WeatherProxy class from your code.&nbsp; You&#8217;ll not need to add a web reference to your project because this proxy class is attributed with the correct SOAP envelope information.</p>
<p>///<br />
/// Soap Format Type.<br />
///<br />
[SoapTypeAttribute("formatType", "http://weather.gov/forecasts/xml/DWMLgen/schema/ndfdXML.xsd")]<br />
public enum formatType<br />
{<br />
    ///<br />
    /// 24 Hour<br />
    ///<br />
    [SoapEnumAttribute("24 hourly")]<br />
    Item24hourly,</p>
<p>    ///<br />
    /// 12 Hour<br />
    ///<br />
    [SoapEnumAttribute("12 hourly")]<br />
    Item12hourly,<br />
}</p>
<p>///<br />
/// Proxy class for weather service.<br />
///<br />
///<br />
/// Uses the NOAA web service, this proxy derived from an example found on the .NET. The proxy that<br />
/// VS2005 generates causes a protocol error/HTTP 502 Bad Gateway.<br />
///<br />
[DesignerCategoryAttribute("code")]<br />
[WebServiceBindingAttribute(Name = "ndfdXMLBinding", Namespace = "http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl")]<br />
public class WeatherProxy : System.Web.Services.Protocols.SoapHttpClientProtocol<br />
{<br />
    #region Construction</p>
<p>    ///<br />
    /// Constructor.<br />
    ///<br />
    public WeatherProxy()<br />
    {<br />
        this.Url = &#8220;http://www.weather.gov/forecasts/xml/SOAP_server/ndfdXMLserver.php&#8221;;<br />
    }</p>
<p>    #endregion Construction</p>
<p>    #region Methods</p>
<p>    ///<br />
    /// Get the weather data by day (synchronously).<br />
    ///<br />
    /// Latitude coord.<br />
    /// Longitude coord.<br />
    /// Start Date.<br />
    /// Number of days.<br />
    /// Time format.<br />
    /// Soap XML<br />
    [SoapRpcMethodAttribute("http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl#NDFDgenByDay", RequestNamespace = "http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl", ResponseNamespace = "http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl")]<br />
    [return: SoapElementAttribute("dwmlByDayOut")]<br />
    public string NDFDgenByDay(<br />
        Decimal latitude, Decimal longitude,<br />
        [SoapElementAttribute(DataType = "date")] DateTime startDate,<br />
        int numDays, formatType format)<br />
    {<br />
        object[] results = this.Invoke(&#8220;NDFDgenByDay&#8221;, new object[] {<br />
                latitude,<br />
                longitude,<br />
                startDate,<br />
                numDays,<br />
                format});<br />
        return ((string)(results[0]));<br />
    }</p>
<p>    /// Creates a  for the specified uri.<br />
    /// The .<br />
    /// The  to use when creating the .<br />
    /// The uri parameter is null.<br />
    protected override WebRequest GetWebRequest(Uri uri)<br />
    {<br />
        HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(uri);<br />
        request.ProtocolVersion = HttpVersion.Version10;<br />
        return request;<br />
    }</p>
<p>    #endregion Methods</p>
<p>}</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/robgarrett.wordpress.com/1822/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/robgarrett.wordpress.com/1822/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/robgarrett.wordpress.com/1822/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/robgarrett.wordpress.com/1822/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/robgarrett.wordpress.com/1822/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/robgarrett.wordpress.com/1822/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/robgarrett.wordpress.com/1822/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/robgarrett.wordpress.com/1822/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/robgarrett.wordpress.com/1822/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/robgarrett.wordpress.com/1822/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/robgarrett.wordpress.com/1822/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/robgarrett.wordpress.com/1822/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/robgarrett.wordpress.com/1822/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/robgarrett.wordpress.com/1822/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/robgarrett.wordpress.com/1822/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/robgarrett.wordpress.com/1822/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.robgarrett.com&amp;blog=7688126&amp;post=1822&amp;subd=robgarrett&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.robgarrett.com/2006/01/24/noaa-weather-service-and-net/feed/</wfw:commentRss>
		<slash:comments>33</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">Rob Garrett</media:title>
		</media:content>
	</item>
	</channel>
</rss>