【问题标题】:Win32 LogonUser doesn't return "Domain Admins" groupWin32 LogonUser 不返回“域管理员”组
【发布时间】:2016-02-10 17:33:44
【问题描述】:

我有一台安装了 AD 的服务器。在此服务器中,我创建了一个用户并将他添加到“域管理员”组。

在 C# 中,我使用了 win32 函数 LogonUser 并使用添加到“域管理员”组的用户进行身份验证。身份验证成功,但是当我使用 WindowsIdentity 和 IdentityReference 检索所有组时,我没有得到“域管理员”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。

【问题讨论】:

    标签: c# active-directory ldap


    【解决方案1】:

    您为什么为此使用 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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-18
      • 2015-06-15
      • 2011-12-27
      • 1970-01-01
      • 2012-03-07
      • 1970-01-01
      • 1970-01-01
      • 2011-06-01
      相关资源
      最近更新 更多