我正在使用System.DirectoryServices.Protocols库查询Active Directory。我有一个用例,需要从服务器检索一个以用户的NT帐户名开头的用户条目(即: NETBIOSDomainName\UserSamAccountName)。这是我使用的代码:
LdapConnection connection = new LdapConnection(userDomain);
NTAccount userAccount = new NTAccount(userDomain, username);
sid = userAccount.Translate(typeof(SecurityIdentifier));
SearchRequest searchRequest = new SearchRequest
{
Scope = SearchScope.Subtree,
Filter = string.Format("(&(objectClass=user)(objectsid={0}))", sid)
};
SearchOptionsControl searchOptions = new SearchOptionsControl(SearchOption.PhantomRoot);
searchRequest.Controls.Add(searchOptions);
SearchResponse response = (SearchResponse)connection.SendRequest(searchRequest);我的问题是我不知道如何在搜索中包括NetBIOSDomainName。如果仅通过用户的samAccountName进行搜索,有时会在响应中得到多个条目,因为相同的SAMAccountName存在于多个域中。
有什么方法可以避免我使用的这个黑客攻击吗?
请注意,我必须使用这个特定的库。
发布于 2014-07-30 03:53:19
如果要查询ActiveDirectory,请使用DirectoryServices。添加对System.DirectoryServices.AccountManagement dll的引用,然后就可以使用示例了,如下所示。
目录服务将允许您轻松地设置您的域作为查找的一部分。下面的示例返回UserPrincipal,它可以用来获取用户帐户的所有详细信息。检查UserPrincipal上所有可用的属性的msdn。
using System.DirectoryServices.AccountManagement;
public UserPrincipal FindUser(string username, string domain)
{
var context = new PrincipalContext(ContextType.Domain, domain);
var user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, username);
// user will be null if not found
// Remember to dispose UserPrincipal once done working with it.
return user;
}发布于 2014-07-30 06:18:29
您可以使用- msdn link重载SearchRequest构造函数,您需要设置第一个参数以将其限制为该域
示例
SearchRequest request = new SearchRequest("DC=mydc,DC=com", sFilter, SearchScope.Subtree);https://stackoverflow.com/questions/25023110
复制相似问题