我需要获取所有组名称及其描述(用户是哪个成员,以及那些没有用户的组)。与外部域的连接必须通过LDAP与端口389以及用户的凭证进行连接。
目前,我可以使用以下代码验证用户:
public string UserValidation(string username, string domain, string password, string url)
{
var credentials = new NetworkCredential(username, password, domain);
var serverId = new LdapDirectoryIdentifier(url);
LdapConnection connection = new LdapConnection(serverId, credentials);
string result = "true";
try
{
connection.Bind();
}
catch (Exception e)
{
result = e.ToString();
}
connection.Dispose();
return result;
}
此link有助于获取群组,但不能用于外部域。
答案 0 :(得分:0)
添加此Namespce
使用System.DirectoryServices;
然后尝试此代码
DirectoryEntry de = new DirectoryEntry(urLDAPdomain, username, passwaord, AuthenticationTypes.Secure);
DirectorySearcher ds = new DirectorySearcher(de);
// in ds u will get all users and groups
答案 1 :(得分:0)
我使用以下代码获得了用户所属的群组描述:
var path = String.Format("LDAP://{0}:{1}", DomainControllerIP, Port);
DirectoryEntry rootDE = new DirectoryEntry(path, strUserName, strPassword);
DirectorySearcher dSearcher = new DirectorySearcher(rootDE);
dSearcher.Filter = "(&(sAMAccountName=" + strUserName + ")(objectClass=User)(objectCategory=Person))";
SearchResult sResult = dSearcher.FindOne();
foreach (var grp in sResult.Properties["memberOf"])
{
string sGrpName = (Convert.ToString(grp).Remove(0, 3)).Split(',')[0];
DirectorySearcher gSearcher = new DirectorySearcher(rootDE);
gSearcher.Filter = "sAMAccountName=" + sGrpName;
SearchResult gResult = gSearcher.FindOne();
//Group Name in groupName
string groupName = gResult.Properties["name"][0].ToString();
}
获取所有组的描述:
dSearcher.Filter = "(&(objectCategory=group))";
dSearcher.PropertiesToLoad.Add("name");
dSearcher.PropertiesToLoad.Add("description");
SearchResultCollection results = dSearcher.FindAll();
foreach (SearchResult res in results)
{
String name = ((res.Properties["name"])[0]).ToString();
string groupDescription = (res.Properties["description"])[0].ToString();
}