C# 无法在C中检索Active Directory用户#
我在Windows2008中构建了一个测试Active Directory服务器,并在其上运行DNS服务器。在运行C#应用程序的客户端计算机上,我可以使用以下功能针对Active directory服务器对用户进行身份验证:C# 无法在C中检索Active Directory用户#,c#,active-directory,ldap,C#,Active Directory,Ldap,我在Windows2008中构建了一个测试Active Directory服务器,并在其上运行DNS服务器。在运行C#应用程序的客户端计算机上,我可以使用以下功能针对Active directory服务器对用户进行身份验证: public static UserPrincipal GetUserPrincipal(string usrName,string pswd,string domainName) { UserPrincipal usr; PrincipalContext ad
public static UserPrincipal GetUserPrincipal(string usrName,string pswd,string domainName)
{
UserPrincipal usr;
PrincipalContext ad;
// Enter Active Directory settings
ad = new PrincipalContext(ContextType.Domain, domainName,usrName,pswd);
//search user
usr = new UserPrincipal(ad);
usr.SamAccountName = usrName;
PrincipalSearcher search = new PrincipalSearcher(usr);
usr = (UserPrincipal)search.FindOne();
search.Dispose();
return usr;
}
在另一个逻辑中,我尝试使用用户名从服务器检索用户。我使用了以下功能:
public static DirectoryEntry CreateDirectoryEntry()
{
// create AD connection
DirectoryEntry de = new DirectoryEntry("LDAP://CN=Users,DC=rootforest,DC=com","LDAP","password");
de.AuthenticationType = AuthenticationTypes.Secure;
return de;
}
public static ResultPropertyCollection GetUserProperty(string domainName, string usrName)
{
DirectoryEntry de = CreateDirectoryEntry();
DirectorySearcher deSearch = new DirectorySearcher();
deSearch.SearchRoot = de;
deSearch.Filter = "(SamAccountName=" + usrName + ")";
SearchResult results = deSearch.FindOne();
return null;
}
但是,LDAP服务器根本没有响应,甚至没有异常。如果我缺少LDAP服务器上的某些设置,你们中的任何人都可以看到我的代码中的缺陷(请不要介意硬代码值,我正在使用此代码进行测试)
作为故障排除的一部分,我确认可以从客户端计算机ping到rootforest.com。我确认存在属性为samaccountname“LDAP”的用户。我的路径似乎是正确的,因为当我进入LDAP服务器并键入:
dsquery user -name LDAP*
我得到以下信息:
CN=LDAP L. LDAP,CN=Users,DC=rootforest,DC=com
任何帮助都将不胜感激,我花了一天的大部分时间对这个小错误进行故障排除和研究,我认为这可能是我忽略的一个小问题。我不明白为什么您在第一个示例中使用新的
PrincipalContext/UserPrincipal
东西,但请回到第二个示例中难以使用的DirectoryEntry
内容。。。。真的没有道理。。。另外:您的第二个函数GetUserProperty
似乎总是返回null
是否键入
既然您已经在使用System.DirectoryServices.AccountManagement
(S.DS.AM)命名空间,那么也可以将其用于第二个任务!请在此处阅读所有相关内容:
public static ????? GetUserProperty(string domainName, string usrName)
{
// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
// find a user
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, usrName);
if(user != null)
{
// return what you need to return from your user principal here
}
else
{
return null;
}
}
新的S.DS.AM使得在广告中与用户和组进行交流变得非常容易:我认为您的代码存在一些问题:
public static ????? GetUserProperty(string domainName, string usrName)
{
// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
// find a user
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, usrName);
if(user != null)
{
// return what you need to return from your user principal here
}
else
{
return null;
}
}
GetUserProperty()
函数中返回null
?您应该返回结果
sSAMAccountName
。此外,将查询扩展为仅搜索用户帐户。下面是一个示例:(&(objectCategory=person)(objectClass=user)(sAMAccountName=usrName))
UserPrincipal
类在Active Directory中搜索标识。UserPrincipal
类提供了一个名为FindByIdentity
()的静态方法来搜索用户身份希望,这会有帮助。谢谢,我想我错删了一行,我曾经有过if-else的情况。返回空值只是我做的一个测试。谢谢你们,我会试试你们的建议。我的returnnull是我调试时留下的一个错误,因为我之前有一个if-else子句,就像你给我看的一样。我同时使用UserPrincipal和DirectoryEntry的原因是为了习惯LDAP的API并学习它们。我将采纳你们两位的建议并进行测试。我稍后会更新我的结果。