BOSS
BOSS

Reputation: 1898

Start process with default credentials

I am running a process using my credentials but , i wanted to use the default credentials , i mean using the current username and password , without going throuh the below code :

var pass = new SecureString();
pass.AppendChar('p');
pass.AppendChar('a');
pass.AppendChar('s');
pass.AppendChar('s');
pass.AppendChar('w');
pass.AppendChar('o');
pass.AppendChar('r');
pass.AppendChar('d');
Process.Start("file.txt", Environment.UserName, pass, "");

Because the application will work on different computers , so it should get the current credentials and use them.

EDIT

The problem is that the file i want to run is a .bat file that will do some cmd commands and it must be provided with a credentials to use

Upvotes: 1

Views: 1835

Answers (3)

Marco
Marco

Reputation: 57573

I don't understand: if you want to execute a process using default credentials, simply don't pass anything:

Process.Start("file.txt");

If you need more complex syntax, redirect input/output, hide window or other things you can use ProcessStartInfo.

EDITED after user edit:
You simply can't recover user password, it's not allowed.
As reported in this link:

You can get the current identity of the user under which the current thread is running (not necessarily the logged in user) using WindowsIdentity.GetCurrent().
Alternatively you can get the logged in user name via the Environment.UserName property.
It is not guaranteed to be the user running the current process however.
There is no Windows API to get a user's password as passwords aren't stored in Windows.
Instead Windows stores a one-way hashed version.

Upvotes: 2

Felice Pollano
Felice Pollano

Reputation: 33252

If there is inside the cmd someone re-asking you for the credentials,there is no way to have the password of the current user programmatically, so the only chanche you have is to ask for the user password again, but this would be probably a security hole.

Upvotes: 0

Alex
Alex

Reputation: 8116

Just use Process.Start(@"Path"). By not providing any credentials, the current user's context is taken.

Upvotes: 1

Related Questions