{"id":737,"date":"2009-06-04T14:33:09","date_gmt":"2009-06-04T22:33:09","guid":{"rendered":"http:\/\/www.curlybrace.com\/words\/?p=737"},"modified":"2010-08-08T13:27:08","modified_gmt":"2010-08-08T20:27:08","slug":"run-external-application-as-another-user-in-c","status":"publish","type":"post","link":"https:\/\/www.curlybrace.com\/words\/2009\/06\/run-external-application-as-another-user-in-c\/","title":{"rendered":"Run External Application as Another User in C#"},"content":{"rendered":"<p>An arbitrary external application can be executed from C# using <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.diagnostics.process.aspx\">System.Diagnostics.Process<\/a>.  If you want to run as another user, setting the System.Diagnostics.Process.StartInfo.Password field can be a bit confusing.  Here is one way using <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.security.securestring.appendchar(VS.85).aspx\">System.Security.SecureString.AppendChar<\/a> to avoid having to resort to unsafe code:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing System;\r\nusing System.Collections.Generic;\r\nusing System.Linq;\r\nusing System.Text;\r\n\r\nnamespace CSRunAs\r\n{\r\n    class Program\r\n    {\r\n        static void Main(string&#x5B;] args)\r\n        {\r\n            System.Diagnostics.Process p = new System.Diagnostics.Process();\r\n            \r\n            \/\/ Domain and User Name:\r\n            p.StartInfo.Domain = &quot;optional_domain&quot;;\r\n            p.StartInfo.UserName = &quot;user_to_run_as&quot;;\r\n\r\n            \/\/ Command to execute and arguments:\r\n            p.StartInfo.FileName = &quot;c:\\\\path\\\\to\\\\executable.exe&quot;;\r\n            p.StartInfo.Arguments = &quot;your argument string&quot;;\r\n            \r\n            \/\/ Build the SecureString password...\r\n            System.String rawPassword = &quot;your_password&quot;;\r\n            System.Security.SecureString encPassword = new System.Security.SecureString();\r\n            foreach (System.Char c in rawPassword)\r\n            {\r\n                encPassword.AppendChar(c);\r\n            }\r\n            \r\n            p.StartInfo.Password = encPassword;\r\n\r\n            \/\/ The UseShellExecute flag must be turned off in order to supply a password:\r\n            p.StartInfo.UseShellExecute = false;\r\n\r\n            p.Start();\r\n        }\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>An arbitrary external application can be executed from C# using System.Diagnostics.Process. If you want to run as another user, setting the System.Diagnostics.Process.StartInfo.Password field can be a bit confusing. Here is one way using System.Security.SecureString.AppendChar to avoid having to resort to &hellip; <a href=\"https:\/\/www.curlybrace.com\/words\/2009\/06\/run-external-application-as-another-user-in-c\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[281,282,15],"tags":[],"class_list":["post-737","post","type-post","status-publish","format-standard","hentry","category-net","category-csharp","category-technology"],"_links":{"self":[{"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/posts\/737","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/comments?post=737"}],"version-history":[{"count":3,"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/posts\/737\/revisions"}],"predecessor-version":[{"id":1241,"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/posts\/737\/revisions\/1241"}],"wp:attachment":[{"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/media?parent=737"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/categories?post=737"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/tags?post=737"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}