开始的处理与用户名和密码 [英] Starting a process with a user name and password
问题描述
我知道,你可以通过以下方式给定的用户名/密码运行进程:
I know that you can run a process with a given username/password in the following way:
var processInfo = new ProcessStartInfo
{
WorkingDirectory = workingDirectory,
FileName = "a name",
UserName = loggedUserName,
Password = "password",
Domain = userNameDomain,
UseShellExecute = false,
};
Process.Start(processInfo);
我现在面临的问题是,我不希望作为一个部分来写的实际密码代码和程序的,如果我离开的密码为空的属性不会开始...我怎样才能安全地启动过程中无需在代码中的硬编码字符串露出了密码?
The problem I'm facing is that I don't want to write the actual password as a part of the code and the process won't start if I leave the Password attribute empty... How can I safely start the process without exposing the password as a hard coded string in the code?
推荐答案
的 ProcessStartInfo.Password 是不是一个简单的字符串,你可以写下来,并分配给属性。你需要的是一个的 SecureString的实例和SecureString的无法创建传递一个简单的字符串给它的构造。显然,操作系统没有API或方法,允许非信任的程序来获取当前用户的密码(这将是最大的安全漏洞没有听说过)。
The ProcessStartInfo.Password is not a simple string that you can write down and assign to the property. What you need is a SecureString instance and a SecureString cannot be created passing a simple string to its constructor. Obviously the OS has no API or method that allows a non trusted program to retrieve the password of the current user (it would be the biggest security bug ever heard of).
所以,在我的思想,你只剩下一个选项。问问你的用户重新输入密码,并由此产生输入应转化为SecureString的
So, in my thinking, you are left with only one option. Ask your user to type again the password and the resulting input should be transformed into a SecureString
这例子是字符串类的我在这里看到
using System.Security;
// ...
public static SecureString ConvertToSecureString(this string password)
{
if (password == null)
throw new ArgumentNullException("password");
unsafe
{
fixed (char* passwordChars = password)
{
var securePassword = new SecureString(passwordChars, password.Length);
securePassword.MakeReadOnly();
return securePassword;
}
}
}
您可以用它来改造密码由用户输入并启动该进程
you could use it to transform the password typed by your user and start the process
using(frmGetPassword fgp = new frmGetPassword())
{
if(DialogResult.OK == fgp.ShowDialog())
{
SecureString ss = fgp.Password.ConvertToSecureString();
var processInfo = new ProcessStartInfo
{
WorkingDirectory = workingDirectory,
FileName = "a name",
UserName = loggedUserName,
Password = ss,
Domain = userNameDomain,
UseShellExecute = false,
};
Process.Start(processInfo);
}
}
这篇关于开始的处理与用户名和密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!