Ocaso Protal
Ocaso Protal

Reputation: 20257

Get the connected server of a PrincipalContext for global catalog

I have a method CreateContextForGlobalCatalog that returns a PrincipalServer that connects to a global catalog:

PrincipalContext = new PrincipalContext(ContextType.Domain,
                                        "forest.name:3268", 
                                        "dc=forest,dc=name", 
                                        ContextOptions.Negotiate, 
                                        userName, password);

Note: That is a reduced version of the method, normally the name and the container are parameters.

With this context I'm looping over objects from the database to get information from the global catalog in ActiveDirectory:

using (PrincipalContext principalContext = CreateContextForGlobalCatalog())
{
    foreach (ADAccount adAccount in accounts){
       Log.Debug("Connected server: " + principalContext.ConnectedServer);

    // get some information from AD here ...
    }
}

The Log.Debug line logs the connected server from the PrincipalContext. I have a test setup containing only virtual machines.

My Problem: When I now disconnect that connected server (disable network adapter) I don't get an exception and get connected to a new server but the log messages still shows the original connected server albeit the server is not available anymore.

Is there a way to somehow refresh the connected server property or to get the information from somewhere else?

Upvotes: 10

Views: 1102

Answers (2)

Itay Podhajcer
Itay Podhajcer

Reputation: 2654

The most simple solution might be to try and ping the remote server using .Net's Ping class.

Upvotes: 0

Hussein Khalil
Hussein Khalil

Reputation: 1401

unfortunately, It will never be updated after initiation, the only workaround you can do is to recheck with every iteration if you are still connected or not

Upvotes: 2

Related Questions