【问题标题】:C# - Error listing all user accounts within Active DirectoryC# - 列出 Active Directory 中的所有用户帐户时出错
【发布时间】: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


    【解决方案1】:

    尝试改变这个(和类似的表达方式):

    (string)child.Properties["givenName"].Value
    

    像这样:

    (string)child.Properties["givenName"]?.Value ?? ""
    

    【讨论】:

    • 感谢您的建议,唉,结果与我上面的原始代码完全相同。
    • 那么您可能需要使用调试器单步执行代码并找到引发错误的确切行。您也可以(暂时)删除 try/catch,因为这隐藏了错误的真实位置。
    • 如果你在 Visual Studio 中运行它,你也应该能够准确地看到哪一行代码引发了这个错误。这与错误消息本身一样重要。
    • 您不需要为此使用特殊的机器。只要用户有足够的权限,任何加入域的机器都可以工作。
    • @Rawns 这意味着child.Properties["userAccountControl"] 会导致null,并且检查null 引用上的属性(即:Value)会导致异常。您必须准备好在那里处理null 结果。
    【解决方案2】:

    我需要做的就是以管理员身份运行 Visual Studio!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多