获取Active Directory用户信息

时间:2016-02-29 13:22:06

标签: c# win-universal-app windows-10-universal

有没有办法检索用户的活动目录信息,如广告组,SID等?

目前我只能找到活动目录azure的示例。

目前我通过以下方式捕获用户信息:

IReadOnlyList<User> users = await User.FindAllAsync(UserType.LocalUser);
foreach (User user in users)
{
    string displayName = (string)await user.GetPropertyAsync(KnownUserProperties.DisplayName);
}

此外,Win10 SDK不提供有关活动目录的任何示例。

[编辑] https://wpdev.uservoice.com/forums/110705-universal-windows-platform/suggestions/12556779-get-active-directory-user-information

上有一个WinRT Api请求

[编辑]关于corefx问题https://github.com/dotnet/corefx/issues/7325

1 个答案:

答案 0 :(得分:0)

在命令行中,您可以使用whoami /groups

在C#中,您可以使用SID来检查组中的成员身份,如下所示:

using System;
using System.Security.Principal;

namespace ADMembership
{
    class Program
    {
        static void Main()
        {
            WindowsPrincipal myPrincipal = new
                WindowsPrincipal(WindowsIdentity.GetCurrent());
            String groupName = "A Group";
            SecurityIdentifier sid = new SecurityIdentifier(
                "S-1-1-22-999999999-999999999-999999999-4444");

            if (myPrincipal.IsInRole(groupName))
            {
                Console.WriteLine("{0} is in {1}.",
                    myPrincipal.Identity.Name.ToString(), groupName);
            }
            else
            {
                Console.WriteLine("{0} is not in {1}.",
                    myPrincipal.Identity.Name.ToString(), groupName);
            }

            if (myPrincipal.IsInRole(sid))
            {
                Console.WriteLine("{0} has SID: {1}.",
                    myPrincipal.Identity.Name.ToString(), sid);
            }
            else
            {
                Console.WriteLine("{0} does not have SID {1}.",
                    myPrincipal.Identity.Name.ToString(), sid);
            }
        }
    }
}