views:

1230

answers:

1

Before trying to query the AD server I would like to check if it is alive and kicking. Looks like a trivial thing, but I haven´t found anything to elucidate this.

How can I do that?

+4  A: 

I just try to get the current domain context associated with the running user:

try {
    var domain = Domain.GetCurrentDomain();
    /* Whatever i need from the domain */
} catch(ActiveDirectoryOperationException ex) {
    MessageBox.Show("Cannot contact AD Server");
}


If you want to connect to another domain you can try:

try {
    var domain = Domain.GetDomain(
        new DirectoryContext(DirectoryContextType.Domain, "mydomain.local"));
    /* Whatever i need from the domain */
} catch(ActiveDirectoryOperationException ex) {
    MessageBox.Show("Cannot contact AD Server");
}
Y Low
That's only going to work if the machine running the code is actually logged into that domain (which doesn't have to be the case).
DannySmurf
check out my update.
Y Low
I was trying to avoid try/catch constructs, but that do the trick!
Seiti