Я наконец нашел решение.
1-е: я проверяю, аутентифицирован ли пользователь. Если true, "goto" 2nd;)
public bool IsAuthenticated(string username, string passwd, string domain)
{
try
{
DirectoryEntry entry = new DirectoryEntry("LDAP://" + domain, username, passwd, AuthenticationTypes.Secure);
DirectorySearcher search = new DirectorySearcher(entry)
{
Filter = "(objectClass=user)",
SearchScope = SearchScope.Subtree
};
SearchResult result = search.FindOne();
return (result != null);
}
catch (Exception ex)
{
throw new Exception("Authentication Error.\n" + ex.Message);
}
2-й: Я проверяю, в какой группе находится пользователь. Мой доступ отличается в основном на 3 группы. Я использовал простое условное выражение, подобное этому:
if (IsAuthenticated(user, password, LdapCmb))
{
// Connection ok
if (AuthenticateGroup(user, password, userLdap, GroupADM))
{
accessRights = "ADM";
// Variable passed to another class (for locking down some contents)
}
else if (AuthenticateGroup(user, password, userLdap, OtherGroup1))
{
accessRights = "OTHER1";
// Variable passed to another class (for locking down some contents)
}
else if (AuthenticateGroup(user, password, userLdap, OtherGroup2))
{
accessRights = "OTHER2";
// Variable passed to another class (for locking down some contents)
}
else
{
MessageBox.Show(this, "You are not authorized to use this application.", MessageBoxButton.OK, MessageBoxImage.Warning);
logIt.AppendText("User " + user + " isn't granted for use");
Application.Current.Shutdown();
}
Close();
}
else
{
MessageBox.Show(this, "You are not authorized to use this application.", MessageBoxButton.OK, MessageBoxImage.Warning);
logIt.AppendText("User " + user + " isn't granted for use");
Close();
}
И, наконец, вот метод «authenticateGroup»:
public bool AuthenticateGroup(string userName, string password, string domain, string group)
{
try
{
DirectoryEntry entry = new DirectoryEntry("LDAP://" + domain, userName, password);
DirectorySearcher mySearcher = new DirectorySearcher(entry)
{
Filter = "(&(objectClass=user)(|(cn=" + userName + ")(sAMAccountName=" + userName + ")))"
};
SearchResult result = mySearcher.FindOne();
foreach (string GroupPath in result.Properties["memberOf"])
{
if (GroupPath.Contains(group))
{
return true;
}
}
}
catch (DirectoryServicesCOMException ex)
{
Console.WriteLine(ex.Message);
}
return false;
}
Это, конечно, не самый лучший метод ... но, по крайней мере, он работает, как и ожидалось в данный момент. Теперь я должен проверить это глубже.
В любом случае, спасибо.