Как найти имя пользователя с помощью Ipaddress или Hostname? - PullRequest
0 голосов
/ 03 января 2019

Я нашел Ipaddress и Hostname, используя активный каталог ComputerPrincipal method.but не найти имя пользователя в этом методе. Мне нужно имя пользователя в моем домене. Как это сделать?

вот мой код

namespace Directory
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var context = new PrincipalContext(ContextType.Domain, "xxx.com"))
            {
                using (var searcher = new PrincipalSearcher(new ComputerPrincipal(context)))
                {
                    foreach (var result in searcher.FindAll())
                    {
                        var auth = result as AuthenticablePrincipal;
                        if (auth != null)
                        {
                            Console.WriteLine("Name: " + auth.Name);
                            Console.WriteLine("Last Logon Time: " + auth.LastLogon);
                            if (auth.Name != "Txx-Cxx-Gxx-Pxx")
                            {
                                IPAddress[] ipaddress = Dns.GetHostAddresses(auth.Name);
                                foreach (IPAddress ipaddr in ipaddress)
                                {
                                    Console.WriteLine(ipaddr);
                                }

                                Console.WriteLine();
                            }
                            else
                            {
                                Console.WriteLine("Error");
                            }
                        }
                    }
                }
            }

Ответы [ 2 ]

0 голосов
/ 03 января 2019

Чтобы войти в Windows Пользователь

string m_strUserName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

Пожалуйста, обратитесь к другим сведениям, предоставленным WindowsIdentity

0 голосов
/ 03 января 2019

Используйте UserPrincipalName свойство класса AuthenticablePrincipal.
Например:

   var auth = result as AuthenticablePrincipal;
   var userName = auth.UserPrincipalName;

См. здесь все свойства, методы и т. Д., Которые AuthenticablePrincipal имеет.

...