Dial VPN connection from a Windows C# service - c#

I am trying to develop a Windows service using C# and Visual Studio 2017. I would like to dial up a pre configured VPN connection from within the service.
If I were to place the following line of code in a C# Windows Desktop app it will quickly flash a CMD prompt and dial the VPN:
System.Diagnostics.Process.Start("rasdial.exe", "ConnectionName user password");
But it does not work in the service. I have brought it to this point. It does not throw an error, it does not dial a connection. It just seems to do nothing as i step through it.
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo = new System.Diagnostics.ProcessStartInfo("rasdial.exe", "ConnectionName user password");
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.ErrorDialog = false;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
process.Start()
Not sure how to fix this or what the next steps would be. An API would be nice if there was one.
I have done some Googling about this but cannot discover anything so i thought i would take it to the experts.
Thanks.
Stephen Simpson

Ken White pointed out that service accounts don't have the same privileges as normal user accounts. I solved the problem by running the service under a specific user.

Related

Print from asp.net

I've got an aps.net site. Can I start a print request for a pdf from a client to a printer connected only to the server?
In my server I use this code in a web api
[HttpPost]
public void Print()
{
ProcessStartInfo info = new ProcessStartInfo();
info.Verb = "print";
info.FileName = #"C:\Users\Me\Documents\Doc.pdf";
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
Process p = new Process();
p.StartInfo = info;
p.Start();
}
If I run this request while debugging it works (i mean it prints the doc correctly), but if I start the request with with postman from a virtual machine (to simulate a network request) and debug the process attached to a IIS site no error will rise but no print done. The debug steps correctly all the instructions.
I set my application pool with my user identity (admin) and "connect as" on the site as well as my user.
I don't know if It can't be done cause on ASP.NET or cause I'm missing something.
This will do. I print with foxit reader. Maybe my default, Acrobat, have some isseus or asking for user interaction.

Everything I start from a worker role has no interface, why?

I am having a very bad moment executing a process from internet explorer.. I am supposed to call internet explorer from a worker role process to perform some operations..but internet explorer is executing with no visible interface...
This is my code :
psi.FileName = "D:\\Program Files\\Internet Explorer\\iexplore.exe";
psi.CreateNoWindow = true;
psi.ErrorDialog = false;
psi.UseShellExecute = false;
psi.WindowStyle = ProcessWindowStyle.Maximized;
psi.RedirectStandardOutput = false;
psi.RedirectStandardInput = false;
psi.RedirectStandardError = true;
psi.Arguments = fileLocation;
exeProcess = Process.Start(psi);
started = true;
exeProcess.PriorityClass = ProcessPriorityClass.RealTime;
Can you please help me? I need to execute internet explorer with a visible interface.
Running process that normally requires completely functioning Windows desktop is unlikely to run correctly from service as there is no UI/windows associated with services. You should be able to reproduce the same behavior by trying to instantiate such application from a service (i.e. IIS) on local machine running under some service account.
If you need to render web pages on server it would be better to either use external service or specifically designed "headless browser" like PhantomJS.

Add ports with netsh in WCF as domain admin without admin privileges

I have a service with WCF in a WPF application (self-hosted) and I have the typical error "Your process does not have access rights to this namespace".
The users can’t have admin privileges so using a .manifest is not a solution.
The ports are dynamic, the application calculate a free port every time is running, so the application must insert the listen port by netsh several times
I use a ProcessStartInfo with domain administrator, but to start the process the user needs admin privileges.
Run the application as administrator neither is a solution, so I need that a normal user can run the application and the program add the port by netsh as domain administrator.
My Process is something like this:
ProcessStartInfo psi = new ProcessStartInfo("netsh", parameter);
SecureString ss = new SecureString();
for (int i = 0; i < adminPass.Length; i++)
ss.AppendChar(adminPass[i]);
psi.Password = ss;
psi.UserName = Admin;
psi.Domain = Domain;
psi.Verb = "runas";
psi.RedirectStandardOutput = false;
psi.CreateNoWindow = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
Process.Start(psi);
Thanks a lot
Take a look at the accepted answer for this Stack Overflow question for a possible solution to your problem. The approach outlined in the answer is to break out the admin-requiring code out into a Windows service which performs the elevated privilege operations under an appropriate (separate) account when invoked.

elevation demand not working for method in winforms app

I have a winforms app that installs other apps in a loop. This works properly on an administrator account in Windows 7, but I have serious issues in a standard account - the app requires elevation in order to write to "Program Files(x86)" folder.
Therefore I am trying to ask for elevation for a specific method (the one that runs the installers) in a winforms c# app, using this code:
[System.Security.Permissions.PrincipalPermission(System.Security.Permissions.SecurityAction.Demand, Role = #"BUILTIN\Administrators")]
After receiving an error, I learned from the web that before calling the method which carries the above attribute, I need to write this:
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
I did this, and the method still throws the following error:
Request for principal permission failed.
Step by step debugging passes the SetPrincipalPolicy line but, when it reaches the method with the Demand atribute, it just throws the same error, as if the SetPrincipalPolicy never existed.
Am I doing something wrong in setting the Demand attribute properly?
Thank you in advance.
LATER EDIT: as requested here is the code that is supposed to trigger the elevation request when installing the app silently (but does not work):
WindowsPrincipal principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
bool hasAdministrativeRight = principal.IsInRole(WindowsBuiltInRole.Administrator);
if (!hasAdministrativeRight)
{
ProcessStartInfo psi = new ProcessStartInfo(file);
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.UseShellExecute = true;
psi.Verb = "runas";
//psi.CreateNoWindow = true;
psi.Arguments = modifiers;
try
{
using (Process process = Process.Start(psi))
{
process.WaitForExit();
if (process.HasExited)
return process.ExitCode;
}
}
catch (Win32Exception wex)
{
}
}
What I need, is for that process to pop a dialog asking for username and password for admin, if the app was ran under a Windows Standard User. Only the process started programmatically above should run as admin, the main app itself can remain as a standard user.
This is just not the way UAC works. It is process based, the user only ever gets the "please let me mess with your machine" prompt when you start a new process. With the proper incantation of "I need the user's consent to mess with the machine, please say Yes" signal embedded in the program. Which you do by this answer.
Death to the idea of making it method based. Unreasonable to a programmer, makes sense to a user. User wins.
You can either force your app to always run as an admin. This is how you do that. It is not recommended however for your app to need admin privileges to run.
If you start a Process to run the installer, you can check here how to run the process as an admin.
A third option which Visual Studio uses is that when you do something where you need admin privileges you are prompted to restart the app and it then restarts the app as an admin and you can perform the tasks. Just use the code from the second way to start your app.
The method you've posted to run as admin will check if the user is admin and then start the process as an admin. If the user doesn't have admin rights the app won't even start. A better solution is to always try to run the process as an admin. Then the user will get an UAC prompt with password and username, which an admin can fill in.
public static int RunAsAdmin(string fileName)
{
ProcessStartInfo psi = new ProcessStartInfo(fileName);
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.UseShellExecute = true;
psi.Verb = "runas";
psi.Arguments = modifiers;
try
{
using (Process process = Process.Start(psi))
{
process.WaitForExit();
if (process.HasExited)
return process.ExitCode;
}
}
catch (Win32Exception wex)
{
}
return 0;
}

Windows SDK - C# - Debugging process exiting with error code -1073741502

SHORT VERSION
How do you figure out which DLL is failing to load (and potentially why) when a process exits with error code -1073741502?
LONG VERSION
I'm trying to write a pretxnchangegroup hook for Mercurial, and as a part of that hook I need to get the output of running the command:
hg log
The code that I'm using to start and run the hg.exe process is as follows:
string Command = "log";
Process p = new Process();
ProcessStartInfo psi = p.StartInfo;
p.StartInfo.FileName = #"C:\Program Files (x86)\Mercurial\hg.exe";
psi.CreateNoWindow = true;
psi.LoadUserProfile = true;
psi.RedirectStandardError = true;
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
psi.WorkingDirectory = Environment.CurrentDirectory;
p.StartInfo.Arguments = Command;
// Pass-through environment variables
psi.UserName = Properties.Settings.Default.HG_User;
psi.Domain = Properties.Settings.Default.HG_Domain;
psi.Password = new System.Security.SecureString();
foreach (char c in Properties.Settings.Default.HG_Pass)
{
psi.Password.AppendChar(c);
}
p.Start();
p.WaitForExit();
The problem is that the process keeps exiting with error code -1073741502, without outputting anything on Standard Output or Standard Error. After some research online, I discovered that this error code has something to do with the application failing to initialize properly (couldn't find DLL's, maybe?), but I have no idea how to go about figuring out how to fix it.
Keep in mind that this hook is being called for when I'm pushing to the repository over the web (so, IIS is calling the Mercurial CGI via Python, which has this program configured as a hook).
In a totally different web application, I'm able to run HG commands just fine, and I'm also able to run this by doing
runas /user:<same account as in the code> /noprofile cmd.exe and then manually typing in the hg command line.
Also, if I set UseShellExecute = true, then it executes just fine, but then I can't get the Standard Output. I'm really tempted to just make a web service call to the web app which is able to execute this command successfully, but that'd be a really ugly solution.
Any ideas why this thing isn't executing?
I was able to resolve this by disabling UAC so it sounds like a permissions problem even though I do not know the exact details.

Categories