C# 按名字和姓氏搜索用户登录id
我发现 当我只有几个用户时,这很有用,但我在AD中有很多用户,所以当我运行我的查询时C# 按名字和姓氏搜索用户登录id,c#,c#-4.0,C#,C# 4.0,我发现 当我只有几个用户时,这很有用,但我在AD中有很多用户,所以当我运行我的查询时 if ((String)(entry.Properties["sn"].Value) == "lname" && (String)(entry.Properties["givenName"].Value) == "fname") { return entry.Properties["samAccountName"].Value.ToString(); } 花了太长时间才完成
if ((String)(entry.Properties["sn"].Value) == "lname"
&& (String)(entry.Properties["givenName"].Value) == "fname")
{
return entry.Properties["samAccountName"].Value.ToString();
}
花了太长时间才完成
如何按名字和姓氏搜索特定的用户登录id 您需要在searcher
上设置属性,然后调用searcher.FindOne()
,而不是searcher.FindAll()
。查询筛选器可以设置为一个对象,您可以在其中设置要搜索的字段
微软在页面上有一个很好的例子,尽管他们的例子希望找到几个符合给定条件的对象
根据您的要求对链接问题进行特别字面上的修改:
using (var context = new PrincipalContext(ContextType.Domain, "mydomain.com"))
{
using (var searcher = new PrincipalSearcher(new UserPrincipal(context) { GivenName = "fname", Surname = "lname" }))
{
foreach (var result in searcher.FindAll())
{
DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;
return de.Properties["samAccountName"].Value.ToString();
}
}
}
如果
entry
是IEnumerable
集合的一部分,则可以执行以下操作:
var entries = {do your population of the collection here}
var entry = entries.Where(e=>e.Properties["sn"].Value.ToString() == "lname"
&& e=>.Properties["givenName"].Value.ToString() == "fname")
.FirstOrDefault();
由于您使用的是.NET4,因此应该检查
System.DirectoryServices.AccountManagement
(S.DS.AM)命名空间。请在此处阅读所有相关内容:
// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
// find a user - by e.g. his "samAccountName", or the Windows user name or something
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");
if(user != null)
{
// do something here....
string samAccountName = user.SamAccountName;
}
如果找不到用户名指定的用户,也可以使用新的搜索功能:
// define a "query-by-example" principal - here, we search for a UserPrincipal
// and with the first name (GivenName) and a last name (Surname)
UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.GivenName = firstName;
qbeUser.Surname = lastName;
// create your principal searcher passing in the QBE principal
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);
// find all matches
foreach(var found in srch.FindAll())
{
// do whatever here - "found" is of type "Principal" - it could be user, group, computer.....
}
新的S.DS.AM使得在广告中与用户和群组进行互动变得非常容易!仅仅找到一个用户也应该相对较快。您应该使用广告服务器进行过滤。为此,请提供LDAP语法筛选器。另外,使用
FindAll
的propertiesToLoad
参数仅指定所需的属性:
public static SearchResultCollection FindByName(
string domain, string firstName, string lastName, string[] properties) {
var rootEntry = new DirectoryEntry("LDAP://" + domain);
var filter = string.Format("(&(sn={0})(givenName={1}))", lastName, firstName);
var searcher = new DirectorySearcher(rootEntry, filter, properties);
return searcher.FindAll();
}
// Using the method:
var result = FindByName("mydomain", "Robert", "Smith", new[] { "samAccountName" })[0];
string uName = (string)result.Properties["samAccountName"][0];
@marc_这正是我想要的,完美,谢谢。。。但是,我确实发现了一个困难的方法,即如果不需要用户请求的任何字段进行搜索,请跳过
UserPrincipal
中相应的属性。我最初按的是string.empty
,然后甚至是null
,没有返回结果。然而,当我添加了一个if
语句并忽略了UserPrincipal
中的该项时,我最终返回了结果(即:在未填充FirstName时跳过GivenName,而不是设置为default)。