Win32 LogonUser 没有 return "Domain Admins" 组

Win32 LogonUser doesn't return "Domain Admins" group

我有一台安装了 AD 的服务器。在此服务器中,我创建了一个用户并将他添加到 "Domain Admins" 组。

在 C# 中,我使用了 win32 函数 LogonUser 并使用添加到 "Domain Admins" 组的用户进行了身份验证。身份验证成功,但是当我使用 WindowsIdentity 和 IdentityReference 检索所有组时,我没有得到 "Domain Admins" Sid。

知道这是为什么吗?

这是我正在使用的代码:

            if (LogonUser(username,
                domainName,
                password,
                (int)LogonType.LOGON32_LOGON_INTERACTIVE,
                (int)LogonProvider.LOGON32_PROVIDER_DEFAULT,
                ref logonToken) != 0)
            {
                WindowsIdentity wi = new WindowsIdentity(logonToken);
                foreach (IdentityReference oneGroup in wi.Groups)
                {
                    GroupList.Add(oneGroup.Value);
                }
                return err;
            }

注意 oneGroup.Value 保存用户所属组的 Sid。

为什么要为此使用 API 调用?您有 System.DirectoryServices.AccountManagement 的时间来执行此操作:

// Vaidate credentials
using (PrincipalContext context = new PrincipalContext(ContextType.Domain))
{
    return context.ValidateCredentials(userName, password);
}

// To get user groups
using (PrincipalContext context = new PrincipalContext(ContextType.Domain))
using (UserPrincipal user = UserPrincipal.FindByIdentity(context, userName))
{
    return user.GetAuthorizationGroups();
}