如何检查给定的Windows用户是否可以登录为服务

问题描述

我必须为Windows安装程序(WiX 3)编写自定义操作,以检查要用于将要安装的Windows服务的Windows帐户。

在这里不能使用LocalSystem帐户提供服务,因为该服务必须使用Windows文件共享(由其UNC路径指定)。

这是我到目前为止编写的代码

public class CustomActions
{
    [CustomAction]
    public static ActionResult VerifyServiceUser(Session session)
    {
        try
        {
            session.Log("VerifyServiceUser: Begin");

            PrincipalContext context = null;
            if (session["SERVICE_USERNAME"].Contains("\\"))
            {
                context = new PrincipalContext(ContextType.Domain);
            }
            else
            {
                context = new PrincipalContext(ContextType.Machine);
            }
            using (context)
            {
                if (context.ValidateCredentials(session["SERVICE_USERNAME"],session["SERVICE_PASSWORD"]))
                {
                    // Todo: Check the user is allowed to login as a service.

                    session["SERVICE_USER_VALID"] = "1";
                }
            }

            session.Log("VerifyServiceUser: End");
        }
        catch (Exception ex)
        {
            session.Log("VerifysqlConnection: exception: {0}",ex.Message);
            throw;
        }

        return ActionResult.Success;
    }
}

我找不到有关如何使用c#进行检查的信息,是否允许用户作为服务登录。这绝对是必要的。

希望您能帮助我解决这个问题。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)