bor
bor

Reputation: 658

Get the "net user /Domain" result through .net

I would like to get the list of all users in the domain using .net approach. When I use the net user /Domain call in cmd I get what I need but in .net I cannot get the list of users nor the address of the domain controller. I get the 'domain does not exist or it's not possible to connect' or 'wrong user name or pass' or 'current security context is not associated with an AD domain or forest' errors...

When I call the net user /Domain it reports the domain on which the search will be conducted and the address of the domain controller. The domain that I can get from .net is different though.

How to get the above net command result in .net? I'm totally new to how the AD works. Any help appreciated.

Update

After some work I was able to get the data I wanted. I was not providing all the details necessary for the calls to succeed. When I included the domain address the system was able to find proper domain controller and fetch the data I needed.

One question remains though.

When running the net user /Domain command I don't have to provide any domain address, the system is able to figure it out on its own.

How to get the domain address automatically through .net?

When I call the Domain.GetDomain( new DirectoryContext( DirectoryContextType.Domain)) I get the Current security context is not associated with an Active Directory domain or forest exception.

Upvotes: 0

Views: 4155

Answers (2)

mati kepa
mati kepa

Reputation: 3231

No

but you can use following command in powershell Get-ADUser "username" -Server "domaincontroller.localdomain"

Upvotes: 0

bor
bor

Reputation: 658

I have made several mistakes on the way to the data I wanted to get and the solution is not a technical one but rather reminder that all the pieces are required for the calls to succeed.

  1. When constructing the PrincipalContext required to interact with the Active Directory service don't forget to include a correct domain address and login and password of a user that is allowed in the domain.

  2. The domain controller address (at least in my case) is the same as DNS server address of the network adapter providing the connection. Instead of providing the domain controller address it's ok to provide only the domain address (or the base DNS server address suffix) and the system will find the domain controller.

The remaining question I have is how to bypass providing the username and password to the PrincipalContext constructor call - after all it's not required by the net user /Domain call. But that's for another time.

Upvotes: 0

Related Questions