C# ActiveDs.dll--使用C查询active directory时应用程序挂起
我在谷歌上搜索这个问题已经有一段时间了,没有发现有人遇到同样的问题。我有一个用C编写的应用程序,它可以完成我们的IT帮助台所需要的许多事情。除了显示有关Active Directory用户信息的部分外,包括NT PW重置和许多远程安装,一切都很正常。我有一个弹出窗口,显示他们的交换信息、锁定状态、组成员身份等 该应用程序是通过ClickOnce安装和更新的,是为.NET Framework 3.5开发的,已验证安装在所有计算机上。当用户单击按钮以显示用户信息时,系统会提示用户输入域名和用户名。然后,如果用户名不完全正确,程序将搜索用户并显示搜索结果。用户选择用户名后,会发生以下两种情况之一: 大约5次中有4次它按预期工作并显示所需信息 整个计划被锁定,需要强制结束 我不完全理解为什么它会锁定,因为在提示输入域名和用户名之前,在初始按钮单击之后完成的所有工作都是在一个单独的线程上完成的。如果这是由某个错误引起的,我认为这将阻止主线程锁定。这个函数和其他一些没有崩溃的AD函数之间的唯一区别是它使用了ActiveDs.dll扩展名,所以我认为它可能与此相关。我也可以根据需要发布代码 这是在新线程中启动广告窗口的代码C# ActiveDs.dll--使用C查询active directory时应用程序挂起,c#,active-directory,C#,Active Directory,我在谷歌上搜索这个问题已经有一段时间了,没有发现有人遇到同样的问题。我有一个用C编写的应用程序,它可以完成我们的IT帮助台所需要的许多事情。除了显示有关Active Directory用户信息的部分外,包括NT PW重置和许多远程安装,一切都很正常。我有一个弹出窗口,显示他们的交换信息、锁定状态、组成员身份等 该应用程序是通过ClickOnce安装和更新的,是为.NET Framework 3.5开发的,已验证安装在所有计算机上。当用户单击按钮以显示用户信息时,系统会提示用户输入域名和用户名。然
private void ADInfoB_Click(object sender, RoutedEventArgs e)
{
string domain = Interaction.InputBox("Domain Name", "Please Enter the domain the user is on");
if (domain != "")
{
string usern = Interaction.InputBox("User Name", "Please enter the NT user name of the user");
if (usern != "")
{
try
{
string userDN = ActiveDirectory.DistinguishedUserName(usern, domain);
if (userDN != null)
{
Thread userInfoT = new Thread(() =>
{
ADWindow ADInfo = new ADWindow();
ADInfo.Show();
ADInfo.ADStartupInfo(usern, domain, userDN);
ADInfo.Closed += (sender2, e2) =>
ADInfo.Dispatcher.InvokeShutdown();
System.Windows.Threading.Dispatcher.Run();
});
userInfoT.SetApartmentState(ApartmentState.STA);
userInfoT.Start();
}
}
catch (Exception a)
{
MessageBox.Show(a.ToString());
}
}
}
}
对ActiveDirectory.DifferentiedUsernameUsern的调用,域;看起来像:
public static string DistinguishedUserName(string user, string LDAPDomain)
{
try
{
threadParams param = new threadParams();
param.usern = user;
param.domain = LDAPDomain;
string distName = string.Empty;
Thread userDistName = new Thread(() =>
{
DSWindow DNuser = new DSWindow();
DNuser.Show();
DNuser.setupUserDN(param);
DNuser.Closed += (sender2, e2) =>
DNuser.Dispatcher.InvokeShutdown();
System.Windows.Threading.Dispatcher.Run();
});
userDistName.SetApartmentState(ApartmentState.STA);
userDistName.Start();
while (userDistName.IsAlive)
{
System.Threading.Thread.Sleep(1000);
}
distName = param.userDN;
return distName;
}
catch (Exception e)
{
MessageBox.Show("" + e);
return "Fail";
}
}
以及实际查找:
public void DistNameUser(object a)
{
threadParams param = a as threadParams;
string usern = param.usern;
string LDAPDomain = param.domain;
string userDN = string.Empty;
string connectionPrefix = "LDAP://" + LDAPDomain;
DirectoryEntry entry = new DirectoryEntry(connectionPrefix);
DirectorySearcher mySearcher = new DirectorySearcher(entry);
UpdateListDelegate ListDelegate = new UpdateListDelegate(UpdateList);
GetIndexDelegate IndexDelegate = new GetIndexDelegate(getIndex);
CloseWindowDelegate cw = new CloseWindowDelegate(closeWindow);
mySearcher.Filter = "(&(objectClass=user)(|(sAMAccountName=" + usern + "*)))";
SearchResultCollection results = mySearcher.FindAll();
foreach (SearchResult res in results)
{
DirectoryEntry tempEnt = res.GetDirectoryEntry();
string strRes = tempEnt.Properties["sAMAccountName"].Value as string;
Dispatcher.Invoke(ListDelegate,
System.Windows.Threading.DispatcherPriority.Background,
new object[] { strRes });
}
SelectionMade.WaitOne();
Dispatcher.Invoke(IndexDelegate,
System.Windows.Threading.DispatcherPriority.Background,
new object[] { param });
SearchResult picked = results[param.index];
DirectoryEntry directoryObject = picked.GetDirectoryEntry();
userDN = "LDAP://" + directoryObject.Properties["distinguishedName"].Value;
param.userDN = userDN;
entry.Close();
entry.Dispose();
mySearcher.Dispose();
Dispatcher.Invoke(cw,
System.Windows.Threading.DispatcherPriority.Background,
new object[] { });
}
如果您想查看更多代码,请告诉我请发布代码示例StackOverFlow上有大量ActiveDirectory示例,可能您查询的内容不正确。。感谢您打开一个用户模式调试器,并尝试查看它正在等待或挂起什么