Start UI on server from web application - c#

I'm trying to get a UI ( browsers like chrome, firefox, IE, ... ) running using a web application.
When i use the following code (on my local machine and on a server)
Process.Start(#"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe");
Process.Start(#"C:\Program Files (x86)\Mozilla Firefox\firefox.exe");
Process.Start(#"C:\Program Files\Internet Explorer\iexplore.exe");
in a console application this is no problem. All three start up with no issues.
class Program
{
static void Main(string[] args)
{
Process.Start(#"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe");
...
}
}
When i try this from a controller method, this does not work.
public class MyController : Controller
{
public ActionResult QuickStart()
{
Process.Start(#"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe");
...
}
}
If i start my own custom executable it does run ( my test executable writes a file to the disk ) but it doesn't show the console that it is supposed to. )
From what i've found this is because the system doesn't have any context as for what user this ui would have to be shown ? So i tried impersonating every possible user ( doesn't work because impersonating isn't the same as getting the current session as a logged in user ? ) and i've tried starting the process for every possible WindowsIdentity possible and yet still notting happend.
Does anyone know what i could do to make this work ?

Whenever your app uses other credentials it creates a new session. To get round this you would need to make a small systray type app so you can login as some account and run this app which listens for commands from your service, and then pops up on that session whatever it is you need it to, and it will be visible to the user that intermediary app runs as.

Related

Wait for an launched application to stop

We are currently working on a project in UWP where we have to start an external application to modify some documents.
We looked into the Windows.System.Launcher API but it seems that we need more than what it can offer us.
As we launched the application from a file, we use the LaunchFileAsync method, based on the example given by the MSDN :
async void DefaultLaunch()
{
// Path to the file in the app package to launch
string imageFile = #"images\test.png";
var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(imageFile);
if (file != null)
{
// Launch the retrieved file
var success = await Windows.System.Launcher.LaunchFileAsync(file);
if (success)
{
// File launched
}
else
{
// File launch failed
}
}
else
{
// Could not find file
}
}
So far, the example suit us well but we also need to be warned when the user is done with the file. The best would be to be able to give the launcher a callback method.
We haven't found anything like that yet in the documentation. Is this even possible ? Do we need to use another solution ?
TL;DR : Is there a solution to open another application from a UWP app and wait for it to return a result object ?
If the external app is also a UWP app then Launcher.LaunchUriForResultsAsync is designed for this. It will launch the target app then wait for the app to call back with the results.
See Launch an app for results for a full walkthrough of how this works.
If the target app isn't a UWP app then you can implement the same thing yourself: both apps declare a protocol. The client launches the server with the server's protocol. When the server's done it notifies the caller by launching the client's protocol.
You might also want to look into App Services which allow a UWP server app to expose a REST-like service to clients on the local system. 
The app process isolation model means you can't do this from a UWP app. As you just want to know when an arbitrary program has finished you could write this in traditional .net/win32 and include that in your UWP app via the desktop bridge.

Windows Service getting access denied exception while writing to Network Drive

I have an interactive windows service which run on a Local System account and with Interact with desktop checkbox checked(this is mandatory for my project as my service needs to invoke .exe with UI ). I am getting an exception as Access denied while writing to network drive. I am passing the UNC path from config file. i tried giving full control access to anonymous user on the folder which i want to access but its still not working. i cannot run my windows service under Network service account or under any other account as suggested in some other posts because i want it interact with desktop check box checked. is there any way to achieve this?
Edit: UNC path of network drive: //server/ABC/pqr
my service should create .txt file in pqr folder. should have access to delete it afterwords too.
i have tried creating anonymous user for pqr folder and giving it full control but still i am getting access denied exception. as i mentioned before i cannot run it under any other account other than local system account because it will automatically disable interact with desktop option in the properties of that service. is there any way to make it run under Network Service Account and still keep it interactive(interact with desktop option checked in the properties of service)?
Try using the following nugget package named SimpleImpersonation
This way you could wrap the code you use to access your remote file location like this:
using (Impersonation.LogonUser(domain, username, password, logonType))
{
// do whatever you want as this user.
}
It worked for me. I used it to turn on and turn off a windows service remotely. Like this:
await Task.Factory.StartNew(() =>
{
using (
Impersonation.LogonUser(serviceInfo.Domain, serviceInfo.User, serviceInfo.Pswd,
Environment.MachineName.Equals(serviceInfo.ComputerName,
StringComparison.InvariantCultureIgnoreCase)
? LogonType.Network
: LogonType.Interactive))
{
var service = new ServiceController(serviceInfo.ServiceName, serviceInfo.ComputerName);
if (service.Status == ServiceControllerStatus.Stopped)
{
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(60));
}
else
{
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(60));
}
}
});
(the snippet was taken from the project site)

How can i open a notepad file (or any process) using webservice deployed in IIS?

How can i open notepad file from another computer using webservice ?
Locally the webservice is running fine and when invoke it opens the notepad file,but when the webservice is deployed in server the notepad file does not get opened.
Code :
//Process
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void notepadopen()
{
Process.Start("F:\\log.txt");
}
Note : Also tried
Process.Start(#"\\192.168.1.8\f\log.txt");
I have already done several methods , some reference SO posts :
Need to execute *.exe in server from ASP.net
Tried setting load user profile to true
Invoke an application using webservice
Foo.cmd won't output lines in process (on website)
Already gave permission to access the folder and file
Also Enabled ASP impersonation
ISAPI and CGI restriction :
Application pool pipeline :
How can i open a notepad file (or any process) remotely using
webservice ?
i can run above program in ASP development server , but cannot run the
same when deployed in IIS
If you are unclear or have found any post related please post them.
Any help would be of great use.Thanks

Launch an executable from within a windows file watcher service

I have a file watch service, written in C#, that I need to launch an application when it detects the file drop. I am using notepad as a test application to launch. The file watcher is working fine, but I cant get notepad to launch. Any assistance with what I am missing would be great.
Code that fires when the file drop is detected:
public void FileCreated(object source, FileSystemEventArgs inArgs)
{
Process LaunchApp = new Process();
LaunchApp.StartInfo.FileName = ConfigurationManager.AppSettings["AppStartPath"];
LaunchApp.Start();
// Process.Start(ConfigurationManager.AppSettings["AppStartPath"]);
Log.WriteLine(" File added: " + DateTime.Now + " " + inArgs.FullPath);
}
Path reference from the app.config:
<add key="AppStartPath" value="Notepad.exe"/>
I also tried:
<add key="AppStartPath" value="C:\Windows\System32\Notepad.exe"/>
I have a file watch service,
Services run in a separate security context to processes in a user logon session.
This can be seen if you add the Session ID column to Task Manager's Processes tab, or – better, in Process Explorer.
Any processes the service launches will run in the service's own context: not the user. There are very good security reasons for this.
To perform interactive operations from a service you need a per user agent that is run in the user's context. Typically the service listens on a named pipe and the user agent is run from the startup group (or Run key in the registry). The agent connects to the named pipe and can respond to requests from the service (or the service from the user agent).

Strange behaviour from System.IO.DirectoryInfo. Exists function

I am developing an application using c# and asp. It need to access some places in the local network . There is a text box in the form which accept the path to be accessed from the user and will store it to a string variable named location.
The if loop always return false if the application run in windows 7. and it occurs only when I run from the installed application, otherwise it will return true if the path is true. Here is the code:
The input to textbox BackupLocation is like this
\\192.168.0.33\Others (F)
. It work fine if the application is hosted on a system which have windows XP
System.IO.DirectoryInfo locationInfo = new System.IO.DirectoryInfo(BackupLocationTxt.Text);
if (locationInfo.Exists) // always return false if the application run in windows 7
{
}
Why this happens ?
This happens because the user you are running your application under doesn't have authorization to read those folders. You might need to grant read access to those folders to the account you are running your site under.
Try System.IO.Directory.Exists(string path) instead.
Your ASP.NET application doesn't have permissions to the folder on other computer in local network.
Try use windows service started under LocalService account.

Categories

Resources