Execute .exe file on server side from local desktop - c#

I have an web application, which was working on server (virtual client). After Publishing and checking from the client side(Locally), I'm getting error.
code I used:
protected void BtnLoadTempTable_Click(object sender, EventArgs e)
{
ProcessStartInfo ps = new ProcessStartInfo();
ps.UseShellExecute = false;
System.Diagnostics.Process.Start(#"D:\Solutions\TFS_Itsme\DEV\WebSolution\WebService\bin\Debug\WebService.exe");
}
Error I got
File not specified

I'm going to answer you questions by describing a good architecture to perform what you are describing. This is designed to be used in a secure production environment.
Requirement:
Allow a remote user to start a process on the server to perform some task.
(You may have other requirements, but at this point all you have given is this one requirement.)
Implementation hardware:
Secure and locked down web server.
Secure and locked down application server
Secure and locked down database server (only be connected to via web and application server
Note, you can run more than one of these on the same physical or virtual server but the processes should be run as different service accounts (users) which have rights to only the resources they need to perform their function.
Data Model
one or more tables which describe the action which needs to take place (for example run example.exe)
one or more tables which describe pending actions to perform (for example, perform an action described in the action table now or in the future)
Use case / Process flow
User connects to website and requests an action be taken (run example.exe) -- this will typically be selected from a list of allowed actions stored in the DB
Website updates the pendingactions table to include information about this request (who asked and when it needs to be run)
User interaction is done.
On the application server (NOT THE WEB SERVER) an agent is checking for actions to perform. That agent sees it needs to run the program -- it does so.
-> Program has been run.
OK -- you are probably looking at this and saying "why"? The importance here is creating a secure environment. The user is never running or using code that runs other code on the server. The user can only pick from a list of "approved" actions -- they don't enter the name of the program. When the program is run it is run in a different context, by a different service account, that has different rights.
You probably noticed this is VERY different than the implementation design you described. This is why I made my comment. No programmer who has a good understanding of secure architecture would ever implement the design you describe. It is very likely such a design would be hacked easily.

Related

How can I create a long running task that listens to requests from the terminal? (Not just asking for input)

I am trying something new to me, so I don't have the vocabulary to express my questions in any sort of domain specific language.
I am currently mind mapping a tool I would like to build. The function of this tool is to execute many long running tasks and log results to an remote database. This is most similar to Jenkins build and test functionality. Unfortunately, I don't think I can use Jenkins since these are tests executed on real live custom hardware with a lot of IO operations with other hardware resources.
It would almost certainly be run on a server, so it's headless. I generally build desktop tools with a UI to execute similar tasks in a windows desktop environment. When I want to communicate to the user what the tool is doing I simply create some UI elements to display status.
In this environment there would rarely if ever be a person looking at it work. If I DO need to debug something while running, or just want to check the status my immediate thought is log files. However, they are pretty cumbersome to watch in real time.
I would like to be able to make requests of the task runner from the command line in the same way I git status. My current thought is to register my command (like git) on PATH. I could have that command connect to a named pipe that my long process is connected to/monitoring and relay the user's request. (I have never used named pipes before, but it seems like a standard way to have processes communicate?)
This solution requires three "layers":
The command that is on PATH that can parse/accept/reject the user's request and then forward it along.
The long process manager that listens for user requests and monitors long task execution.
The task executers themselves.
Are there other approaches? Am I reinventing the wheel? Links and resources are greatly appreciated!
How can I create a long running task that listens to requests from the
terminal? (Not just asking for input)
You can use sockets for inter-process communication or pipes.
Am I reinventing the wheel?
It depends on the use of that program. ie what are the tasks that are running.
For ex: If you are creating a version control system then yes, git already exists
If not then no. You are simply creating a program or application or website that use multiple threads. Multiple threads are provided by the hardware (CPU) and Operating system. You Interact with these threads using the programming languages.
Applications or programs can be single or multi threaded.
For example github:
When you vote on an answer you are basically creating a request that is received by a program running on a server which creates a thread to update the vote count in the database. In the meanwhile if someone else also vote on an answer a similar request is sent and handled maybe by the same server on another thread. This server is still listening for request and in the same time executing tasks (updating the vote count in the database)
Note: This example is simplified, just as an example
For running tasks in the background and still having a functioning UI or request listener, you require multiple threads. One thread for UI, one or more for handling requests, and the rest for running those background tasks and writing to the logs.
Note:
when dealing with threads thread safety and shared resources between threads are important.Also programs can also use more than multiple processes, but multithreading was introduced to fix some of the overhead of using multiple processes (related to thread safety and shared resources).
In your case, If the log files are in a database, then concurrency is access is handled by that database.
But in case the log file is a local file on the system, then writing and reading from the log requires handling the multiple threads access to that one log file. That is where thread safety and shared resources is used.
Note that the below is a simplified comparision:
In case of a local application which uses multiple processes:
This application is like a network of processes, with each process
running a certain program.
Process 1 might render the UI, Process 2 might write to a log file,
etc...
Pipes, signals, or other operating system technologies are used to
send messages and communicate between these processes (Programs).
Ex: when a user press "Write log" button, process 1 communicates with
process 2 (using the communication methods from above) to tell
process2 to write to that log file.
In the case of a web app:
processes becomes computers connected over the network(or servers
running on a host) Operating system technologies becomes Network
communication like HTTP request and responses, JSON, xml, etc...
So This application is like a network of computers (servers)
located in different locations, with each computer running a certain
program.
Computer 1 might render the UI, computer 2 might write to the
database, etc...
Network communication technologies are used to communicate between the
computers or servers over the internet. Ex: when a user press
"Write log" button, Computer 1 communicates with Computer 2 (using the
communication methods from above) to tell Computer to write to that
database.
A host is a computer hosting (running) a program. This program is usually called a server. (The host provides services)

Handling multiple deployments of application ASP.NET

I have a product, and a front end website where people can purchase the product. Upon purchase, I have a system that creates an A record in my DNS server that points to an IP address. It then creates a new IIS website with the bindings required.
All this works well, but I'm now looking at growing the business and to do this I'll need to handle upgrades of the application.
Currently, I have my application running 40 websites. It's all the same code base and each website uses it's own SQL Server database. Each website is ran in a separate application pool and operate completely independently.
I've looked at using TeamCity to build the application and then have a manual step that runs MSDeploy for each website but this isn't particularly ideal since I'd need to a) purchase a full license and b) always remember to add a new website to the TeamCity build.
How do you handle the upgrade and deployments of the same code base running many different websites and separate SQL Server databases?
First thing, it is possible to have a build configuration in TeamCity that builds and deploys to a specific location...whether a local path or a network drive. I don't remember exactly how but one of the companies I worked with in Perth had exactly the same environment. This assumes that all websites are pointing to the same physical path in the file system.
Now, a word of advice, I don't know how you have it all setup, but if this A record is simply creating a subdomain, I'd shift my approach to a real multi-tenant environment. That is, one single website, one single app pool for all clients and multiple bindings associated to a specific subdomain. This approach is way more scalable and uses way less memory resources...I've done some benchmark profiling in the past and amount of memory each process (apppool) was consuming was a massive waste of resources. There's a catch though, you will need to prepare your app for a multi-tenant architecture to avoid any sort of bleeding such as
Avoiding any per-client singleton component
Avoiding static variables
Cache cannot be global and MUST a client context associated
Pay special attention to how your save client files to the file system
Among other stuff. If you need more details about setting up TeamCity in your current environment, let me know. I could probably find some useful info

WPF Window is not Opening from IIS [duplicate]

I want to run an exe on client system from my c# asp.net website. When I use Process.Start()
it throws an error:
The requested operation requires elevation.
How do I set permissions to run that exe?
You can't spawn processes on the client machine from server-side code.
When you use Process.Start in server-side code, it is attempting to execute the process there, on the server where the website is hosted. If you wanted to create processes on the clients computer then you would need to expose a download for them (and not in employing subterfuge, like malign sites might do to install software - supply it gracefully, and normally (and with permission)), or a Silverlight application or something along those lines.
The bottom line is that the code you want to execute (even if that is just to spawn a process) must reside on the client, and be executed there.
You can't run an application from a web server like that. You will have to have the user download the application by supplying the EXE, a setup file or using ClickOnce.
Or you can develop an ActiveX control that you can have the browser automatically download from a Trusted Internet Zone.
Once downloaded, proper signing with a certificate (signed from the trusted (corporate) root certificate) will avoid the user getting a prompt to ask whether he wishes to allow the ActiveX control to install/be activated -
The ActiveX control can subsequently do anything the interactively logged on user could. This means that to actually install a program you'd need to elevate (UAC on Vista+); But if the goal was just to run a standalone executable, you should be good to go.
This all assumes white-hat purposes in a (larger) corporate setting, because it relies on PKI infrastructure and central browser policies, to name just two.**
This would, really, lead to some excellent questions on serverfault or superuser
I noticed you said you wanted to run an exe file on the client, but you didn't say explicitly that the exe is on the server and you want to push it to the client. Everyone seems to be assuming that is the case.
You CAN accomplish this fairly easily with a small JavaScript if you have a few prerequisites:
The executable is already present on the client machine.
All of your clients are running IE
You can enforce a policy to put your site in the Intranet or Trusted
Sites zone.
So basically this means it's a corporate intranet application. I am assuming this is probably the case since, well, if you were expecting to do this with a public app, I would be surprised.
For the script to accomplish this, please see my answer to this question:
How can I get a program on a client machine to run from an ASP.NET page?

Guidance for app to be installed at client's machines, storing config data and scheduling tasks

I have a web app and want to transfer data from client's machines to us every day. Assume there is a common API on every client machine to extract data from. To make this work, I have to create:
An API to receive data from clients - using WCF, seems ok at this point
An application that's installed on client machines
The client app needs to store info from the user (eg username/password to access our API - encrypted with DPAPI). The app needs to run daily (probably with a random Sleep() command so our API isn't overloaded all at once). It also needs to be easy to install.
I've created a console app which talks with the client API and our own API. I've used Visual Studio's Settings.settings with a user scope to save the persistent settings - if parameters are provided then it stores these settings, if no parameters it uses the stored settings.
How can I make this usable for the end user? I'm thinking a separate installer/configuration program that installs the exe file (and its dependencies) and asks the user to enter the settings to be stored (which can also be read by the client app). It would have to set up the scheduled task and also offer the ability to change the configuration (the stored shared variables).
Hoping someone can help architect this solution?
Thanks so much!
I think that your idea about an installer is correct since you will most likely have dependencies or prequisites to install.
However, rather than building the settings logic into the installer, I would recommend that you build a UI for this in your application so that the user can adjust it post-installation if needed.
For example, if the user changes their password, in your current design, the user will have to uninstall and reinstall the app. Also, if the scheduled time is incompatible with some other operations on their machine, then they will need to adjust the time without uninstalling and reinstalling.
You could build the UI and API interface into a single application: just change the behavior (runtime or configuration) with a command line switch (for example, only use a /runtime command line switch for the scheduled task).

Monitor running apps in .NET?

We have several apps that run on a Windows 2003/2008 server. Some of these apps are Java JAR files that are kicked off with a Scheduled Task using a app.cmd file.
Others are "big ones" like SQL Server and IIS.
I'd like to write an app (or service, actually) that simply monitors those programs and sees if they are running or not.
This is a little beyond what I've done before. Oh, this needs to be written in C#.
I was thinking of some type of "heart beat" pattern so that every few minutes, I check if the thread is running (again, new advanced threading) and if so, send out an "All's OK" message (using SMTP or something).
Any tips where to get started?
Thanks for any suggestions.
You can use Process.GetProcesses():
Use this method to create an array of new Process components and associate them with all the process resources on the local computer.
You should probably handle each application on a case-by-case basis, for exaple although you could just check for the existance of the SQL Server process you would be better off running a query that (for example) checks for the existance of a given database in the server.
Similarly you could run a simple HTTP request against an IIS server to check to make sure that the permissions are set up correctly etc...
Obviously the way that you test your Java processes would depend on what they do, although you could still just check for to see if the process is running (be aware that if they are running as a service they may be running inside one of the svchost.exe processes - this doesn't apply to scheduled tasks though).

Categories

Resources