检查Windows帐户的用户名和密码

时间:2022-11-15 13:09:11

I have an installation package that installs a service process that I create. I'd like to prompt the user for the username/password of the account that the service process should run under. I'd like to verify the the username/password combination are valid before continuing with the installation. I have a C DLL that I am using for special installation behavior, but I can't figure out how to use the Windows API to verify an account's credentials. I'd like to be able to support the same account name syntax used by the service control manager.

我有一个安装包,用于安装我创建的服务进程。我想提示用户输入服务进程应运行的帐户的用户名/密码。在继续安装之前,我想验证用户名/密码组合是否有效。我有一个C DLL,我用于特殊安装行为,但我无法弄清楚如何使用Windows API来验证帐户的凭据。我希望能够支持服务控制管理器使用的相同帐户名称语法。

3 个解决方案

#1


12  

The function you want to use is LogonUser. You can even be extra-cool and specify the LOGON32_LOGON_SERVICE flag which checks to make sure the user has the appropriate permissions to run a service.

您要使用的功能是LogonUser。您甚至可以非常酷,并指定LOGON32_LOGON_SERVICE标志,该标志会检查以确保用户具有运行服务的适当权限。

#2


2  

LogonUser is the canonical way to do this, though Microsoft somewhat discourages it.

LogonUser是这样做的规范方式,尽管微软有点不鼓励它。

#3


0  

I've implemented this using the LogonUser function as you guys have mentioned (by the way, this service requires WinXP SP2 or later so I'm not worried about the privilege issue). However, this isn't quite working as I had hoped. If I call QueryServiceConfig, lpServiceStartName is in the format ".\accountname". If I pass this string as is to LogonUser, it fails. I assume the portion before the '\' represents the machine on which the user belongs?

我已经使用LogonUser函数实现了这个,正如你们提到的那样(顺便说一下,这个服务需要WinXP SP2或更高版本,所以我不担心特权问题)。但是,这并不像我希望的那样完全正常。如果我调用QueryServiceConfig,则lpServiceStartName的格式为“。\ accountname”。如果我将此字符串传递给LogonUser,则会失败。我假设'\'前面的部分代表用户所属的机器?

Also, if I call ChangeServiceConfig specifying "LocalSystem" and "" for the lpServiceStartName and lpPassword parameters respectively, this works fine. However, calling LogonUser with these parameters does not work.

另外,如果我分别为lpServiceStartName和lpPassword参数调用ChangeServiceConfig指定“LocalSystem”和“”,这样可以正常工作。但是,使用这些参数调用LogonUser不起作用。

I'd really like to use the same syntax that the SCM uses for the account names.

我真的想使用SCM用于帐户名称的相同语法。

#1


12  

The function you want to use is LogonUser. You can even be extra-cool and specify the LOGON32_LOGON_SERVICE flag which checks to make sure the user has the appropriate permissions to run a service.

您要使用的功能是LogonUser。您甚至可以非常酷,并指定LOGON32_LOGON_SERVICE标志,该标志会检查以确保用户具有运行服务的适当权限。

#2


2  

LogonUser is the canonical way to do this, though Microsoft somewhat discourages it.

LogonUser是这样做的规范方式,尽管微软有点不鼓励它。

#3


0  

I've implemented this using the LogonUser function as you guys have mentioned (by the way, this service requires WinXP SP2 or later so I'm not worried about the privilege issue). However, this isn't quite working as I had hoped. If I call QueryServiceConfig, lpServiceStartName is in the format ".\accountname". If I pass this string as is to LogonUser, it fails. I assume the portion before the '\' represents the machine on which the user belongs?

我已经使用LogonUser函数实现了这个,正如你们提到的那样(顺便说一下,这个服务需要WinXP SP2或更高版本,所以我不担心特权问题)。但是,这并不像我希望的那样完全正常。如果我调用QueryServiceConfig,则lpServiceStartName的格式为“。\ accountname”。如果我将此字符串传递给LogonUser,则会失败。我假设'\'前面的部分代表用户所属的机器?

Also, if I call ChangeServiceConfig specifying "LocalSystem" and "" for the lpServiceStartName and lpPassword parameters respectively, this works fine. However, calling LogonUser with these parameters does not work.

另外,如果我分别为lpServiceStartName和lpPassword参数调用ChangeServiceConfig指定“LocalSystem”和“”,这样可以正常工作。但是,使用这些参数调用LogonUser不起作用。

I'd really like to use the same syntax that the SCM uses for the account names.

我真的想使用SCM用于帐户名称的相同语法。