目前,我有以下内容,从 LDAP 中提取。
// Get context, based on currently logged on user
PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, Environment.UserDomainName);
// Search for the "Domain Users" group, with domain context
GroupPrincipal group = GroupPrincipal.FindByIdentity(domainContext, IdentityType.SamAccountName, "Domain Users");
if(group != null) {
List<Principal> members = null;
members = group.GetMembers(false)
.OrderBy(principal => principal.Name).ToList();
List<Principal> Principals = members
.OrderBy(a => a.Name.Split(',')[0])
.ThenBy(a => a.Name.Split(',')[1])
.ThenBy(a => a.SamAccountName).ToList();
}
我遇到的问题是,如果我有 Doe,John,它可以正常工作,但如果我有 Admin01,它会崩溃,因为它找不到第二部分。我如何更改它,以便如果它只有一个词,它也可以工作?