我有一项服务,安装后会将 Myapp.exe.config 文件(根据 app.config 文件生成)放入安装目录中。在安装过程中,我需要此文件中的详细信息来配置安装。特别是,我需要调整服务的 ServiceProcessInstaller 实例的帐户/用户名/密码字段,以便在它运行时以特定用户身份运行。
但是,在安装过程中,安装程序尚未设置我的注册表设置,也没有将 Myapp.exe.config 复制到安装目录...所以我无法提取这些值,这意味着我的服务无法安装作为正确的用户。我现在能做到的唯一方法是将用户/传递值硬编码到 ProjectInstaller 类中,但我无法让自己这样做。这是错误的。
是否有关于如何以特定用户身份安装服务以及如何在安装过程中访问这些凭据的 Windows 安装最佳实践。
目前我正在尝试类似的事情:
namespace MyService
{
[RunInstaller(true)]
public partial class ProjectInstaller : System.Configuration.Install.Installer
{
public ProjectInstaller()
{
InitializeComponent();
//Set the user credentials.
//NOTE: Eventually this needs to be updated to pull these values out of a
// conf file. The problem now is that the location of the conf file
// is tied to a registry entry for the location of the service which
// may or may not exist when this block is executed.
/* This is the only way I can get it to work, but this is
* too horrible to ever actually do it this way:
serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.User;
serviceProcessInstaller1.Username = "DOMAIN\\user";
serviceProcessInstaller1.Password = "password";
*/
// Try to pull the service's registry values to know where it installed:
RegistryKey keyService = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\services\\MyService");
string path = ((string)keyService.GetValue("ImagePath"));
path = path.Substring(1, path.LastIndexOf('\\'));
string user = someValueFromFileIOontheAbovePath1,
pass = someValueFromFileIOontheAbovePath2;
serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.User;
serviceProcessInstaller1.Username = user;
serviceProcessInstaller1.Password = pass;
//Doesn't work because install isn't complete and there aren't reg settings
// yet, and even if I had the path the .config file is not written yet so
// there's no file to parse.
}
}
}
一些保存这些参数的 .config 文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="User" value="DOMAIN\bob"/>
<add key="Password" value="secret"/>
</appSettings>
</configuration>
如何在安装过程中提取这些用户/密码值,而不需要将它们硬编码到二进制文件中?
谢谢。
最佳答案
有许多选项具有不同的安全级别。
1) 如果您指定 ServiceAccount.User 但不提供用户名和密码,Microsoft 的类将弹出一个窗口以供输入凭据。这是非常安全的,因为凭据永远不会存储在任何地方,但弹出窗口并不能真正让运行安装程序的人清楚他们正在输入的内容。另外,弹出窗口与其他安装屏幕的配合并不是很好。
2) 您可以让 MSI 将用户名和密码作为参数传递给您的安装程序类。不过,这些值以纯文本形式保留在 InstallLog 文件中,因此您肯定要删除它。
3) 让 MSI 使用 DPAPI(System.Security.Cryptography.ProtectedData 类)加密注册表中的名称/密码,然后解密它们并在服务安装程序类中使用它们。
关于c# - 在安装时设置 .NET Windows 服务的角色...从 app.config 设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10588671/