I want to do Active Directory Operations over SSL and port 696 and use this test code:
var domain = "server:636";
var domainPath = "DC=x,DC=y,DC=c";
var username = @"abc";
var userSearch = @"abc2";
var password = @"password";
using (var context = new PrincipalContext(ContextType.Domain, domain, domainPath, ContextOptions.Negotiate | ContextOptions.SecureSocketLayer,username,password))
{
UserPrincipal p = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, userSearch);
var groups = p.GetGroups(context);
foreach (GroupPrincipal g in groups)
{
Console.WriteLine(g.ToString());
}
}
I get a Exception at p.GetGroups(context)
:
Ein Ausnahmefehler des Typs "System.Runtime.InteropServices.COMException" ist in System.DirectoryServices.dll aufgetreten.
Zusätzliche Informationen: Der Server ist nicht funktionstüchtig.
After checks at the firewall we realized, that the first part is handled over LDAPS and SSL and the getGroups call is handled over LDAP without SSL and is getting blocked by firewall.
I also tried
var groups = p.GetGroups();
and many other variations. But i dont get the problem.
Edit 20160614 - Workaround:
var entry = (DirectoryEntry)user.GetUnderlyingObject();
int propertyCount = entry.Properties["memberOf"].Count;
for (int propertyCounter = 0; propertyCounter < propertyCount; propertyCounter++)
{
var dn = (String)entry.Properties["memberOf"][propertyCounter];
var equalsIndex = dn.IndexOf("=", 1, StringComparison.Ordinal);
var commaIndex = dn.IndexOf(",", 1, StringComparison.Ordinal);
if (-1 == equalsIndex)
{
break;
}
var groupName = (dn.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1));
var groupPrincipal = GroupPrincipal.FindByIdentity(principalContext, groupName.Replace("\\",""));
// Do something with your groupPricipal
}