我正在使用一个 asp.net Web 应用程序。我使用表单身份验证来验证来自活动目录的用户。一切正常。但是,我很困惑为什么我应该将 AdsObject 绑定到一个从未使用过的本机对象。我的代码是这样的
DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd);
string[] uName;
try
{
//Bind to the native AdsObject to force authentication.
object obj = entry.NativeObject;
DirectorySearcher search = new DirectorySearcher(entry);
uName = domainAndUsername.Split('\\');
search.Filter = "(SAMAccountName=" + uName[1] + ")";
search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();
if (null == result)
{
return false;
}
//Update the new path to the user in the directory.
_path = result.Path;
_filterAttribute = (string)result.Properties["cn"][0];
}
catch (Exception ex)
{
throw new Exception("Error authenticating user. " + ex.Message);
}
我什至评论了这条线并对其进行了测试。它也工作得很好。我到处寻找答案,从未找到答案。希望我能在这里得到它.. :)