【发布时间】:2012-04-03 11:35:23
【问题描述】:
所以,我有这个方法,在最后一行,它试图获取 samAccountName,它向我抛出一个 COM 异常,这让我抓狂。
有什么想法吗?
public User FindUsername(string samAccountName, string groupDisplayName)
{
using (DirectoryEntry searchRoot = new DirectoryEntry(ldapf, ldapu, ldapp))
{
using (DirectorySearcher searcher = new DirectorySearcher(searchRoot))
{
searcher.Asynchronous = false;
searcher.PropertiesToLoad.Add("SAMAccountName");
searcher.PropertiesToLoad.Add("displayName");
searcher.PropertiesToLoad.Add("uSNChanged");
searcher.PropertiesToLoad.Add("member");
searcher.PropertiesToLoad.Add("co");
searcher.PropertiesToLoad.Add("company");
searcher.PropertiesToLoad.Add("mail");
searcher.Filter = String.Format("(SAMAccountName={0})", samAccountName);
searcher.SearchScope = SearchScope.Subtree;
searcher.PageSize = 1000;
SearchResult result = searcher.FindOne();
ResultPropertyCollection resultPropColl = result.Properties;
Object memberColl = resultPropColl["member"];
using (DirectoryEntry memberEntry = new DirectoryEntry("LDAP://" + memberColl, ldapu, ldapp))
{
try
{
System.DirectoryServices.PropertyCollection userprops = memberEntry.Properties;
object obVal = userprops["SAMAccountName"].Value;
【问题讨论】:
-
Server is not operation COMException.
-
检查你的属性,SAMAccountName 应该是 sAMAccountName(链接:msdn.microsoft.com/en-us/library/windows/desktop/…)
-
我在 sharepoint 2010 的计时器服务中运行相同的代码,完全一样。
-
可能是域控制器宕机了? (链接:weblogs.asp.net/pabloperalta/archive/2011/12/22/…)
-
ldapf 是什么格式的?
标签: c# active-directory directoryservices