【发布时间】:2020-10-13 15:45:42
【问题描述】:
我正在尝试使用 Windows 表单列出 Active Directory 中所有基于用户的帐户的一些基本详细信息(带有基于帐户是否启用/禁用的自定义图标)。运行时,我收到此初始错误,并留下第二个屏幕截图:
由于某种原因,该图标位于第 1 列而不是第 4 列,我最终只有一个帐户。我的代码:
try
{
var domainName = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName;
MessageBox.Show(domainName);
using (var context = new PrincipalContext(ContextType.Domain, domainName))
{
using (var searcher = new PrincipalSearcher(new UserPrincipal(context)))
{
foreach (var result in searcher.FindAll())
{
ListViewItem tmpItem = null;
DirectoryEntry child = result.GetUnderlyingObject() as DirectoryEntry;
switch (child.SchemaClassName)
{
case "user":
int flag = (int)child.Properties["userAccountControl"].Value;
bool disabled = Convert.ToBoolean(flag & 0x0002);
if (disabled == false)
{
tmpItem = new ListViewItem(new string[] {
(string)child.Properties["givenName"].Value,
(string)child.Properties["sn"].Value,
(string)child.Properties["samAccountName"].Value
}, (int)AdImages.User);
this.listView_ad.Items.Add(tmpItem);
}
else
{
tmpItem = new ListViewItem(new string[] {
(string)child.Properties["givenName"].Value,
(string)child.Properties["sn"].Value,
(string)child.Properties["samAccountName"].Value
}, (int)AdImages.block);
this.listView_ad.Items.Add(tmpItem);
}
break;
case "organizationalUnit":
break;
case "container":
break;
case "computer":
break;
case "group":
break;
default:
break;
}
}
}
}
}
catch (Exception e)
{
MessageBox.Show("ERROR : " + e, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
编辑:删除 try / catch 以获得完整错误:
************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
at ComputeamPasswordManager.PasswordManager.getUsers()
at ComputeamPasswordManager.PasswordManager.PasswordManager_Load(Object sender, EventArgs e)
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
编辑 2: 来自在域设备上运行的 Visual Studio 的屏幕截图...
【问题讨论】:
标签: c# winforms active-directory