2

Using C# .Net4.0, WinForms (not ASP.NET) on Windows 7

Can I get the same dialogue as you get when you shift-right-click an executable and then select "Run as different user"?

I want to prompt the user for credentials, but pass the details off to the OS (ie. so my app never sees or handles username/password). I was hoping to use the built-in prompt instead of rolling my own.

I am aware of the LogOnUser function to get a token to use in the WindowsIdentity class, but I figured there might be an alternate API I could call so I didn't need to make my own login prompt.

The end goal here is to test if the user is in a given Windows Group (Custom groups, not built-in ones). I am also concerned that this will require Administrator rights based on the MSDN sample code, but the account this will be deployed on will not necessarily be an Admin (my application does not require Admin rights at the moment and I'd like to keep it that way if possible).

leppie
  • 115,091
  • 17
  • 196
  • 297
HodlDwon
  • 1,131
  • 1
  • 13
  • 30

1 Answers1

3

You can use the "runasuser" Shell verb, like this:

ProcessStartInfo psi = new ProcessStartInfo(YourExePath);
psi.Verb = "runasuser";
Process.Start(psi);
Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
  • This didn't work because I couldn't get return values from the Shell because it returns a null `Process`. So I ran with the MSDN sample code http://msdn.microsoft.com/en-us/library/chf6fbt4%28v=vs.100%29.aspx **Note:** you can use `Environment.MachineName` to get local PC and `Environment.UserDomainName` to get the current user's domain for the 'domain' string parameter. – HodlDwon Apr 10 '14 at 21:10
  • @Good info, thanks Josh. I've just hit your original problem and had the same issue with this answer (null process). Did you ever find a more elegant solution than capturing the desired username and P/Invoking? – Basic Mar 25 '15 at 16:34
  • @Basic Nope, I ended up creating a whole slew of authentication code in my common library to use in my projects. Cobbled together from MSDN, SO answers and a very helpful example https://gist.github.com/mayuki/339952 on Git. My helper classes are spread across many files, so it'd take me some time to post it as a coherent answer here, but all the info is out there (I didn't have to invent anything new). – HodlDwon Apr 30 '15 at 21:23