C# 使用 DirectoryEntry 启用用户帐户

C# enable user account with DirectoryEntry(C# 使用 DirectoryEntry 启用用户帐户)
本文介绍了C# 使用 DirectoryEntry 启用用户帐户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想知道用户帐户是否启用.我使用此代码:

I want to know if user account enable. I use this code:

  var usersList = new List<DirectoryEntry>();
  DirectoryEntry localMachine = new DirectoryEntry("WinNT://" + computerName + ",Computer", Settings.UserName,Settings.UserPassword);

  DirectoryEntry admGroup = localMachine.Children.Find(Settings.AdministratorsGroup, "group");

   object members = admGroup.Invoke("members", null);

   foreach (object groupMember in (IEnumerable)members)
   {
        DirectoryEntry member = new DirectoryEntry(groupMember);
        var b =  member.Properties["userAccountControl"].Value; // <---- value == null
        usersList.Add(member);
   }

我正确获取了所有成员.
但是member.Properties["userAccountControl"].Value出现错误.我知道使用 System.DirectoryServices.AccountManagement 命名空间,但我想知道为什么此代码不起作用.

I get all members correctly.
But an error is appeared in member.Properties["userAccountControl"].Value. I know of using System.DirectoryServices.AccountManagement namepsace, but i want to know why this code doesn't work.

推荐答案

您正在使用 WinNT:// 提供程序,它的功能非常有限.它不支持成熟的 LDAP:// 提供程序具有的许多常用属性 - 我认为这可能是此代码设置 userAccountControl 的原因(这是一个 LDAP 属性,很可能不存在并且不支持本地用户帐户)不起作用.

You're using the WinNT:// provider, which is very limited in its abilities. It doesn't support many of the usual properties that the full-blown LDAP:// provider has - and I would think that's probably the reason why this code setting userAccountControl (which is an LDAP attribute, most likely not present and not support on a local user account) doesn't work.

请参阅 Richard Mueller 的文章 WinNT 与 LDAP 以了解有关 内容的更多信息WinNT:// 能做(或不能做)

See Richard Mueller's article WinNT vs. LDAP for a lot more information on what WinNT:// can do (or cannot do)

这篇关于C# 使用 DirectoryEntry 启用用户帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

What#39;s the difference between retrieving WindowsPrincipal from WindowsIdentity and Thread.CurrentPrincipal?(从 WindowsIdentity 和 Thread.CurrentPrincipal 检索 WindowsPrincipal 之间有什么区别?)
How do I find a user#39;s Active Directory display name in a C# web application?(如何在 C# Web 应用程序中查找用户的 Active Directory 显示名称?)
How can I authenticate against Active Directory in Nancy?(如何在 Nancy 中对 Active Directory 进行身份验证?)
How to get a username in Active Directory from a display name in C#?(如何从 C# 中的显示名称获取 Active Directory 中的用户名?)
Working with DirectoryServices in ASP.NET Core(在 ASP.NET Core 中使用 DirectoryServices)
Create Active Directory user in .NET (C#)(在 .NET (C#) 中创建 Active Directory 用户)