<?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>stuartpreston.net &#187; Development Practices</title>
	<atom:link href="http://stuartpreston.net/blog/category/development-practices/feed/" rel="self" type="application/rss+xml" />
	<link>http://stuartpreston.net/blog</link>
	<description>Blog of Stuart Preston</description>
	<lastBuildDate>Sun, 23 Oct 2011 09:34:01 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Running and debugging a .NET  application as Network Service in Visual Studio 2010</title>
		<link>http://stuartpreston.net/blog/2010/05/18/running-and-debugging-a-net-application-as-network-service-in-visual-studio-2010/</link>
		<comments>http://stuartpreston.net/blog/2010/05/18/running-and-debugging-a-net-application-as-network-service-in-visual-studio-2010/#comments</comments>
		<pubDate>Tue, 18 May 2010 10:04:07 +0000</pubDate>
		<dc:creator>Stuart Preston</dc:creator>
				<category><![CDATA[Development Practices]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://stuartpreston.net/blog/2010/05/18/running-and-debugging-a-net-application-as-network-service-in-visual-studio-2010/</guid>
		<description><![CDATA[This post generated some interest on a discussion list so I’m re-posting here on my blog.  Cutting a longer story short, the original poster wanted a way to start a process as Network Service rather than as the current Windows identity to mitigate the risk that you have more rights than you should have. 
For those [...]]]></description>
			<content:encoded><![CDATA[<p>This post generated some interest on a discussion list so I’m re-posting here on my blog.  Cutting a longer story short, the original poster wanted a way to start a process as Network Service rather than as the current Windows identity to mitigate the risk that you have more rights than you should have. </p>
<p>For those who have tried to use “runas” to achieve this you are left with the rather bleak message: “RUNAS ERROR: Unable to acquire user password”. </p>
<p><img style="display: inline; border: 0px;" title="image" src="http://stuartpreston.net/blog/wp-content/uploads/2010/05/image2.png" border="0" alt="image" width="700" height="369" /></p>
<p>I’ll skip the reasons for this and go straight onto the solution.  Create a dummy service using the Service Manager (sc.exe).  This launches the application in the right session and is allowed to run as Network Service or even Local System (NT Authority\Local System).</p>
<blockquote><p>@echo off<br />
sc delete NetworkServiceCmd &gt;nul:<br />
sc create NetworkServiceCmd binpath= &#8220;cmd /K start c:\neverendingapp.exe&#8221; obj= &#8220;NT Authority\Network Service&#8221; &gt;nul:<br />
sc start NetworkServiceCmd &gt;nul:<br />
sc delete NetworkServiceCmd &gt;nul:</p></blockquote>
<p>Simply drop the above into a batch file and run it.  You can then use “Attach to Process” in Visual Studio to attach to the process (don’t forget that your PDB’s must be in the same location as the assembly).</p>
<p><img style="display: inline; border: 0px;" title="image" src="http://stuartpreston.net/blog/wp-content/uploads/2010/05/image3.png" border="0" alt="image" width="720" height="310" /></p>
<p>Here’s a screenshot of the debugger attached to my “NeverEndingApp” and viewing the current identity.</p>
<p><img style="display: inline; border: 0px;" title="image" src="http://stuartpreston.net/blog/wp-content/uploads/2010/05/image4.png" border="0" alt="image" width="720" height="522" /></p>
<p>You need to manually kill off the process you started, you can use Task Manager as usual or here’s a macro I use that works in my scenario.  You can attach this to a Keyboard Shortcut or even a Toolbar.</p>
<blockquote><p>Imports System<br />
Imports EnvDTE<br />
Imports EnvDTE80<br />
Imports EnvDTE90<br />
Imports EnvDTE90a<br />
Imports EnvDTE100<br />
Imports System.Diagnostics</p>
<p>Public Module TerminateProcess</p>
<p>    Public Function TerminateProcess()</p>
<p>        Dim processes As EnvDTE.Processes = DTE.Debugger.LocalProcesses<br />
        Dim process As EnvDTE.Process</p>
<p>        For Each process In processes<br />
            If (process.Name.ToLower().Contains(&#8220;neverendingapp.exe&#8221;)) Then<br />
                process.Terminate(True)<br />
            End If<br />
        Next</p>
<p>End Function<br />
End Module</p></blockquote>
<p>I’m certain all the above can be improved on but I think all the basics are in here and customising it for your process is a challenge I’ll leave to the reader!</p>
]]></content:encoded>
			<wfw:commentRss>http://stuartpreston.net/blog/2010/05/18/running-and-debugging-a-net-application-as-network-service-in-visual-studio-2010/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

