How to accessing admin local data location from non-admin user? - c#

I have an installer that will run as adminstrator. On installing that exe in non-admin system account, some files shipped in administrator local appdata location. I need to access admin local appdata location (C:\Users{ admin user name}\AppData\Local) using process in console app that is in non-admin account.
Process proc = new Process();
proc.StartInfo.FileName = #"C:\Users\{admin user name}\AppData\Local";
proc.Start();
But current user local appdata location opened in file explorer while run. Could you please help me to resolve this?

Using the below code works for me!!!
Process.Start("explorer.exe",#"C:\Users\{admin user name}\AppData\Local");

Related

Restarting IIS website, copying new files with external program

I have website (.net core 3.1) up on IIS (v10) on Windows (10)
website is calling external .exe that will stop IIS website (caller website), copy new files to iis website folder and start the site again. Problem is, when i stop website from external exe it doesn't shut down the website like admin does, then external program can't copy new files to website folder.
When i stop website manually i can copy and overwrite files it works as it should
Here is a snipped of website code that is calling external .exe:
using (var process = new Process())
{
process.StartInfo.FileName = _configuration.GetValue<string>("AppSettings:UpdaterPath");
process.StartInfo.Arguments = args;
process.StartInfo.CreateNoWindow = false;
process.StartInfo.UseShellExecute = true;
process.Start();
}
here is a snippet of my external exe code:
ServerManager server = new ServerManager();
var site = server.Sites.FirstOrDefault(s => s.Name == _settings.WebsiteName);
if (site != null)
{
//Cloning old version to backup
Log($"Copying old files to backup...");
CloneDirectory(_settings.WebsitePath, Path.Combine(_settings.BackupPath, _settings.WebsiteName));
//Copying new version to working dir
//if(!Directory.Exists(temp_folder)) Directory.CreateDirectory(temp_folder);
Log($"Extracting new files...");
ZipFile.ExtractToDirectory(temp_zip, temp_folder, true);
Log($"Stopping {_settings.WebsiteName}...");
site.Stop();
Log($"Copying new files...");
CloneDirectory(Path.Combine(_settings.FilesPath, "temp"), _settings.WebsitePath);
Log($"Starting {_settings.WebsiteName}...");
site.Start();
if (site.State == ObjectState.Started)
{
server.ApplicationPools[site.Applications["/"].ApplicationPoolName].Recycle();
}
}
CloneDirectory() just copies/overwrites files recursively.
I've tried stoping website pool, recycling, server.CommitChanges(), but nothing works because main dll files with real changes to website can't be copied because **
access is denied
**
this all works fine when i call exe as administrator
I've tried giving folder/files user privileges to IIS_IUSRS and my website default pool
I've tried giving website physical path credentials as administrator
I've tried giving website pool privileges identity set to admin acc, or NetworkService, LocalService
basically any combo i can think off
what i noticed is when i stop app pool in my exe it stops working after a few seconds
I assume there is a problem with privileges (cant stop iis, copy files etc)

%LocalAppData% Path for User in Service Context in C#

We have an app that can run as a command line executable or a windows service (via ServiceBase). It depends on user-specific config files that are stored in a subdirectory of that returned by
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData).
When run as a cmd exe, it returns the user's %localappdata% it's fine. However, when installed and run as a windows service, the user is LocalSystem and the above code returns
C:\Windows\System32\config\systemprofile\AppData\Local
We'd like to avoid using RunAsAService perms (we have verified that it works), but we need to get the logged-on user's %localappdata% directory. How do we go about getting that directory for the logged-on user, rather than the system user?

IIS 8( windows server 2012) cannot run batch file

Im hosting an asp.net application in IIS8 on windows server 2012.
this application suppose to execute an batch file.
it works perfect if i execute the application with visual studio in debug mode. but when i upload it to the IIS the application cant execute the batch file.
i tried to change the batch file with exe file. same problem.
the batch file suppose to execute from the application(WCF application) that in the IIS:
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.WorkingDirectory = #"C:\path";
proc.StartInfo.FileName = #"C:\path\executer.bat";
proc.Start();
proc.Close();
please help,
Most likely the problem you are experiencing is due to permissions. You need to check what account the IIS Application Pool that your app is running under is configured for and whether that account has rights to run your batch file or the commands within that batch file.
When running in Visual Studio you're likely running IIS Express as the interactive user.
When running under IIS by default you're running under ApplicationPoolUser identity which has no rights to execute code, has no file access or anything else. Unless you've explicitly set a different account with appropriate file access rights to the batch file, and rights to execute the commands inside of the batch file, you won't be able to run the batch file from within IIS. To change that change the Application Pool user identity to a user that does have rights to both read and execute the batch file on disk and has any rights required to run what's executing in the batch file.
Make sure any folder or file accessed by your application have permissions granted to the AppPoolUser account. It is also important to check your applications resource folders too. For example, if you are writing logs, make sure you give the appPool user account enough permission to write to that file.
Your code is working fine . May be there is some issue in batch file i faced before .My batch file is
start "" BATCHLOG.exe
where BATCHLOG.exe is the executable
Some times batchfile named only
BATCHLOG.exe
does not work correctly on windows scheduler so may be in that case of yours

How to find the path to appdata folder for the logged in user in windows service

I wrote a service and in the code I tried to get the AppData folder's path:
C:\Users\[Username]\AppData\
I tried:
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
But I got:
C:\Windows\system32\config\systemprofile\AppData
You're getting the AppData folder of the Account running the service. (System Account)
There is no other user involved in it.
If you want a particular user's AppData folder, run the Windows Service under that user's account.

Under which user account does Process.Start start?

I run on the machine as admin, but with the UAC set to default mode.
I start an installation program (using "Run as administrator"). From the installation process (using Wix) I start a client program that creates some files on the disc (C:\ProgramData...).
var startInfo = new ProcessStartInfo()
{
WorkingDirectory = installLocation,
FileName = fullPath
};
Process.Start(startInfo);
This first time the program runs, I can access all the data stored on the local disc without problems.
If I close it and start it again, I receive this error message:
Access to the path 'C:\ProgramData...' is denied.
If I start again the application using "Run as administrator" I can access the files from local disc; no error this time. The access path error appears only when I start the application directly.
Is this due to the settings from UAC or it is related that the local files where created under a more privileged user account?
Yes, the application will run without elevated privileges which are required to access ProgramData and other sensitive system folders or user related folders.
Any process you spawn from within your application will inherit the privileges your application has.
You need to create a manifest for your application to request elevated rights upon starting so you will have access to those folders.
Yes, as you say the local files are created under a more privileged user account.
You can check this passing the admin credential with ProcessStartInfo
var startInfo = new ProcessStartInfo()
{
WorkingDirectory = installLocation,
FileName = fullPath,
UserName = "Administrator",
Password = "password"
};
You can bypass this simply creating the ProgramData folder with lower privileges:
Add "Everyone" privilege to folder using C#.NET
or you can force that the application has to be started with administrator rights..
Project - Add New Item - "Application Manifest File".
change
<requestedExecutionLevel>
to
<requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
When starting a new process, it inherits the privileges of the current process.
So if your process was elevated, the created process is elevated too. Second time you run your program, if it's not elevated, the spawned process won't be too.
You can specify the runas verb to force the display of the UAC pop-up that will allow to manually elevate your spawned process:
ProcessStartInfo startInfo = new ProcessStartInfo
{
UseShellExecute = true,
FileName = "cmd.exe",
Verb = "runas"
};

Categories

Resources