简体   繁体   中英

Get Users from Active Directory running on Windows Server in Azure Cloud

I'd like to read out all users on an active directory running on a windows server 2016 inside a azure cloud. I can access the windows server with remote desktop but I'm not able to use the PrincipalContext of the DirectoryServices to connect to the AD. I have the IP-Address of the remote windows server 2016 on which the active directory service is running. A user with admin rights was also created.

I tried different statements regarding the connection of the PrincipalContext.

1.

using (var adContext = new PrincipalContext(ContextType.Domain, "ip-adress of remote server"))
using (var adContext = new PrincipalContext(ContextType.Domain, "LDAP://ip-adress of remote server"))
 using (var context = new PrincipalContext(ContextType.Domain, "ip-adress of remote server", "CN=Users", "adminuser", "adminpassword")

The exception I'm getting is System.DirectoryServices.AccountManagement.PrincipalServerDownException: 'The server could not be contacted. Is there an issue because the server is not in the same domain?

This is my first time doing anything regarding active directory or windows server. So if there is some giant error in in my approach please be considerate.

Sounds like a firewall issue - you just can't contact the server over the network. You can test this in PowerShell using the command:

Test-NetConnection "ip-adress of remote server" -Port 389

There are 4 ports you can test:

  • 389: The default LDAP port. This is used if you don't specify a port.
  • 636: LDAPS, or LDAP over SSL (encrypted)
  • 3268: Global Catalog. Works the same as LDAP, but includes all domains in your forest (if you have more than one domain)
  • 3269: GC over SSL

If any of the other ports work, then you can specify it in the PrincipalContext :

new PrincipalContext(ContextType.Domain, "ip-adress of remote server:636")

Using SSL complicates things a little since your computer needs to trust the SSL certificate that the server sends.

If none of the ports work, then there is a firewall blocking communication somewhere and you need to get that fixed first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM