Ramya k

Ramya k

  • NA
  • 14
  • 2.7k

how to find the username using Ipaddress or Hostname?

Jan 3 2019 1:02 AM
I have find the Ipaddress and Hostname using the active directory ComputerPrincipal method.but not find the username in this method.i need the username in my domain .how to do this?
 
here it my code
 
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;
var userName = auth.UserPrincipalName;
if (auth != null)
{
Console.WriteLine("Name: " + auth.Name);
Console.WriteLine("Last Logon Time: " + auth.LastLogon);
Console.WriteLine("UserName: " + auth.UserPrincipalName);
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");
}
}
}
}
Console.ReadLine();

Answers (2)