<?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/"
	>

<channel>
	<title>Aneef.Net &#187; User Account Control</title>
	<atom:link href="http://www.aneef.net/tag/user-account-control/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.aneef.net</link>
	<description>Do it in .Net way &#124; Blogging about C#,ASP.Net, LINQ,WPF and .Net Technologies</description>
	<lastBuildDate>Mon, 24 May 2010 10:50:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Request UAC Elevation for .net Application (Managed Code)</title>
		<link>http://www.aneef.net/2009/06/29/request-uac-elevation-for-net-application-managed-code/</link>
		<comments>http://www.aneef.net/2009/06/29/request-uac-elevation-for-net-application-managed-code/#comments</comments>
		<pubDate>Mon, 29 Jun 2009 15:35:48 +0000</pubDate>
		<dc:creator>Aneef Fashir</dc:creator>
				<category><![CDATA[Security]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[visual studio 2008]]></category>
		<category><![CDATA[Administrator]]></category>
		<category><![CDATA[Application Manifest]]></category>
		<category><![CDATA[mt.exe]]></category>
		<category><![CDATA[Permission]]></category>
		<category><![CDATA[UAC]]></category>
		<category><![CDATA[User Account Control]]></category>
		<category><![CDATA[Vista]]></category>
		<category><![CDATA[Windows 7]]></category>
		<category><![CDATA[WindowsPrincipal]]></category>

		<guid isPermaLink="false">http://www.aneef.net/2009/06/29/request-uac-elevation-for-net-application-managed-code/</guid>
		<description><![CDATA[Its about time for us to get ready for Windows 7. and most of us have to develop applications compatible to Windows 7, Vista, and Server 2008. and sometimes we need applications to run with administrative privileges. and that&#8217;s not a big deal. but in these new Operating Systems, even an administrator is sometimes locked [...]]]></description>
			<content:encoded><![CDATA[<p>Its about time for us to get ready for <a href="http://www.microsoft.com/windows/windows-7/" target="_blank">Windows 7</a>. and most of us have to develop applications compatible to Windows 7, Vista, and Server 2008. and sometimes we need applications to run with administrative privileges. and that&#8217;s not a big deal. but in these new Operating Systems, even an administrator is sometimes locked by the <a href="http://en.wikipedia.org/wiki/User_Account_Control" target="_blank">UAC</a> when running some applications.</p>
<p>some applications might throw exceptions in such situation, when we need administrative privileges to do some task, if they don’t request for UAC elevation by the application. only option to make these application is to right click and select “Run as Administrator” to manually trigger UAC.</p>
<p>In this post I’m going to show how to request for UAC Elevation for a .net Application. this explains the use of <a href="http://msdn.microsoft.com/en-us/library/aa374191(VS.85).aspx" target="_blank">Application Manifests</a> that request the OS for UAC elevation. </p>
<p>&#160;</p>
<h2>Code</h2>
<p>I have my sample windows form C# Project with a single form that shows a message box on form load,showing whether you are an administrator or not.</p>
<p>&#160;</p>
<pre class="csharpcode">  <span class="kwrd">private</span> <span class="kwrd">void</span> Form1_Load(<span class="kwrd">object</span> sender, EventArgs e)
        {
         <span class="kwrd">bool</span> isAdmin = <span class="kwrd">new</span> WindowsPrincipal(WindowsIdentity.GetCurrent())</pre>
<pre class="csharpcode">                   .IsInRole(WindowsBuiltInRole.Administrator) ? <span class="kwrd">true</span> : <span class="kwrd">false</span>;

            <span class="kwrd">if</span>(isAdmin)
            {
                MessageBox.Show(<span class="str">&quot;you are an administrator&quot;</span>);
            }
            <span class="kwrd">else</span>
            {
                MessageBox.Show(<span class="str">&quot;You are not an administrator&quot;</span>);
            }
        }</pre>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>&#160;</p>
<p>in the above code we use <a href="http://msdn.microsoft.com/en-us/library/system.security.principal.windowsprincipal.aspx" target="_blank">WindowsPrincipal</a> class, which allows us to check the Windows group membership of a Windows user. to use this classes un need to use the following namespace in the code:</p>
<pre class="csharpcode"><span class="kwrd">using</span> System.Security.Principal;</pre>
<pre class="csharpcode">&#160;</pre>
<p>when you compile this application and run , you would expect that it will show the message saying “you are an administrator” if you are logged in as an administrator account. well this is what you get if you have UAC enabled, not something you expected right ?</p>
<p><a href="http://www.aneef.net/wp-content/uploads/2009/06/image.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.aneef.net/wp-content/uploads/2009/06/image_thumb.png" width="221" height="156" /></a> </p>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>ok now we need to make our application to request for UAC elevation.&#160; first we need to create a manifest file with the application name.</p>
<h2>Create Manifest File</h2>
<p>First add a new item to the project with the name with following format, &lt;applicationname&gt;.exe.manifest . in our case (ElevationTest.exe.manifest). and add the following XML:</p>
<pre class="csharpcode"><span class="kwrd">&lt;?</span><span class="html">xml</span> <span class="attr">version</span><span class="kwrd">=&quot;1.0&quot;</span> <span class="attr">encoding</span><span class="kwrd">=&quot;UTF-8&quot;</span> <span class="attr">standalone</span><span class="kwrd">=&quot;yes&quot;</span>?<span class="kwrd">&gt;</span>
<span class="kwrd">&lt;</span><span class="html">assembly</span> <span class="attr">xmlns</span><span class="kwrd">=&quot;urn:schemas-microsoft-com:asm.v1&quot;</span> <span class="attr">manifestVersion</span><span class="kwrd">=&quot;1.0&quot;</span><span class="kwrd">&gt;</span>
   <span class="kwrd">&lt;</span><span class="html">assemblyIdentity</span> <span class="attr">version</span><span class="kwrd">=&quot;1.0.0.0&quot;</span>  <span class="attr">name</span><span class="kwrd">=&quot;ElevationTest&quot;</span> <span class="attr">type</span><span class="kwrd">=&quot;win32&quot;</span><span class="kwrd">/&gt;</span>
      <span class="kwrd">&lt;</span><span class="html">trustInfo</span> <span class="attr">xmlns</span><span class="kwrd">=&quot;urn:schemas-microsoft-com:asm.v3&quot;</span><span class="kwrd">&gt;</span>
      <span class="kwrd">&lt;</span><span class="html">security</span><span class="kwrd">&gt;</span>
         <span class="kwrd">&lt;</span><span class="html">requestedPrivileges</span><span class="kwrd">&gt;</span>
            <span class="kwrd">&lt;</span><span class="html">requestedExecutionLevel</span> <span class="attr">level</span><span class="kwrd">=&quot;requireAdministrator&quot;</span><span class="kwrd">/&gt;</span>
         <span class="kwrd">&lt;/</span><span class="html">requestedPrivileges</span><span class="kwrd">&gt;</span>
      <span class="kwrd">&lt;/</span><span class="html">security</span><span class="kwrd">&gt;</span>
   <span class="kwrd">&lt;/</span><span class="html">trustInfo</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">assembly</span><span class="kwrd">&gt;</span></pre>
<p>&#160;</p>
<p>and the next step is to add the manifest in to the built executables Win32Resources by embedding it.&#160; to do this we need to use the MT.exe in the .Net SDK.&#160; by default it can be found in “C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\”.</p>
<p>we can use the following command to embed the manifest through visual studio command line:</p>
<p><strong>mt.exe –manifest MyApp.exe.manifest -outputresource:MyApp.exe;1</strong> </p>
<p>or </p>
<p><strong>mt.exe –manifest MyLibrary.dll.manifest -outputresource:MyLibrary.dll;2</strong> </p>
<p>(1 for an EXE, 2 for a DLL.) </p>
<p>to make things easier we can add this command to the postbuild event of the project :</p>
<p><a href="http://www.aneef.net/wp-content/uploads/2009/06/image1.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.aneef.net/wp-content/uploads/2009/06/image_thumb1.png" width="549" height="328" /></a><br />
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
</p>
<p>below is the full&#160; post build command:</p>
<p>&quot;C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\mt.exe&quot; -manifest &quot;$(ProjectDir)$(TargetName).exe.manifest&quot;&#160; –outputresource:&quot;$(TargetDir)$(TargetFileName)&quot;;#1</p>
<p>once you have built the application successfully if you see the output directory you will see the icons will have a shield on them indicating that the application requires UAC elevation.</p>
<p><a href="http://www.aneef.net/wp-content/uploads/2009/06/image2.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.aneef.net/wp-content/uploads/2009/06/image_thumb2.png" width="322" height="159" /></a> </p>
<p>And now if you run your built application it will prompt for UAC : and when you click on allow the application will show you the following Message : </p>
<p>&#160;</p>
<p><a href="http://www.aneef.net/wp-content/uploads/2009/06/image3.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.aneef.net/wp-content/uploads/2009/06/image_thumb3.png" width="199" height="156" /></a> </p>
<p>That’s it you have your application ready to request UAC Elevation in the new operating systems.</p>
<p>&#160;</p>
<p>hope it helps.</p>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fwww.aneef.net%2f2009%2f06%2f29%2frequest-uac-elevation-for-net-application-managed-code%2f"><img border="0" alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.aneef.net%2f2009%2f06%2f29%2frequest-uac-elevation-for-net-application-managed-code%2f" /></a></p>
<p><!-- BEGIN SMOWTION TAG - 468x60 - aneef net: tecnologia - DO NOT MODIFY --><br />
<script type="text/javascript"><!--
smowtion_size = "468x60";
smowtion_section = "642940";
//-->
</script><br />
<script type="text/javascript" src="http://ads.smowtion.com/ad.js">
</script><br />
<!-- END SMOWTION TAG - 468x60 - aneef net: tecnologia - DO NOT MODIFY --></p>
<img src="http://www.aneef.net/?ak_action=api_record_view&id=87&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.aneef.net/2009/06/29/request-uac-elevation-for-net-application-managed-code/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
