查找 AD 中是否启用或禁用用户帐户

2024-01-08

我需要查找在 AD 中是否启用或禁用用户帐户。

我找不到旗帜或财产“用户帐户控制”。 这可以使用 USERPRINCIPAL 类来实现吗?

        drop_persona1.Items.Clear();
        string valor = drop_area.SelectedValue;

            List<string> allUsers = new List<string>();

       PrincipalContext ctx2 = new PrincipalContext(ContextType.Domain, "xxxxxxxx",
                                                        valor);


            UserPrincipal qbeUser2 = new UserPrincipal(ctx2);
            qbeUser2.Enabled = true; // activo para autenticacion

            PrincipalSearcher srch2 = new PrincipalSearcher(qbeUser2);
            srch2.QueryFilter = qbeUser2;    

            foreach (var found2 in srch2.FindAll().OrderBy(x=> x.DisplayName))
            {
                ListItem lst_user = new ListItem(found2.DisplayName, found2.SamAccountName);
                drop_persona1.Items.Insert(drop_persona1.Items.Count, lst_user);
            }

        //}
    }

Regards


我没有测试过这个答案,但我相信它应该有效。

1)使用 - 获取目录条目对象

UserPrincipal qbeUser2 = new UserPrincipal(ctx2);
var dirEntry = qbeUser2.GetUnderlyingObject() as DirectoryEntry;

2)然后通过以下方式检查帐户禁用状态 -

var status = IsAccountDisabled(dirEntry);
public static bool IsAccountDisabled(DirectoryEntry user)
        {
            string Uac = "userAccountControl";
            if (user.NativeGuid == null) return false;

            if (user.Properties[Uac] != null && user.Properties[Uac].Value != null)
            {
                var userFlags = (UserFlags)user.Properties[Uac].Value;
                return userFlags.Contains(UserFlags.AccountDisabled);
            }

            return false;
        }

3)这是枚举用户标志 -

[Flags]
public enum UserFlags
{
    // Reference - Chapter 10 (from The .NET Developer's Guide to Directory Services Programming)

    Script = 1,                                     // 0x1
    AccountDisabled = 2,                            // 0x2
    HomeDirectoryRequired = 8,                      // 0x8
    AccountLockedOut = 16,                          // 0x10
    PasswordNotRequired = 32,                       // 0x20
    PasswordCannotChange = 64,                      // 0x40
    EncryptedTextPasswordAllowed = 128,             // 0x80
    TempDuplicateAccount = 256,                     // 0x100
    NormalAccount = 512,                            // 0x200
    InterDomainTrustAccount = 2048,                 // 0x800
    WorkstationTrustAccount = 4096,                 // 0x1000
    ServerTrustAccount = 8192,                      // 0x2000
    PasswordDoesNotExpire = 65536,                  // 0x10000 (Also 66048 )
    MnsLogonAccount = 131072,                       // 0x20000
    SmartCardRequired = 262144,                     // 0x40000
    TrustedForDelegation = 524288,                  // 0x80000
    AccountNotDelegated = 1048576,                  // 0x100000
    UseDesKeyOnly = 2097152,                        // 0x200000
    DontRequirePreauth = 4194304,                   // 0x400000
    PasswordExpired = 8388608,                      // 0x800000 (Applicable only in Window 2000 and Window Server 2003)
    TrustedToAuthenticateForDelegation = 16777216,  // 0x1000000
    NoAuthDataRequired = 33554432                   // 0x2000000
}

Update

这是在 AD 上测试的完整代码。在我的测试中效果很好。

using System;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;

namespace DisableUsers
{
internal class Program
{
    private static void Main()
    {
        const string sAMAccountName = "vikas"; // The sAMAccountName of AD user
        var principalContext = new PrincipalContext(ContextType.Domain, "domainNameHere", "AdminUser", "AdminPass");
        var userPrincipal = UserPrincipal.FindByIdentity(principalContext, sAMAccountName);

        if (userPrincipal != null)
        {
            var dirEntry = userPrincipal.GetUnderlyingObject() as DirectoryEntry;
            var status = IsAccountDisabled(dirEntry);
            Console.WriteLine(status ? "Account {0} is disabled." : "Account {0} is enabled.", sAMAccountName);
        }
        else
        {
            Console.WriteLine("No user found for sAMAccountName '{0}'.", sAMAccountName);
        }

        Console.ReadLine();
    }

    public static bool IsAccountDisabled(DirectoryEntry user)
    {
        const string uac = "userAccountControl";
        if (user.NativeGuid == null) return false;

        if (user.Properties[uac] != null && user.Properties[uac].Value != null)
        {
            var userFlags = (UserFlags)user.Properties[uac].Value;
            return userFlags.Contains(UserFlags.AccountDisabled);
        }

        return false;
    }
}

public static class UserFlagExtensions
{
    /// <summary>
    /// Check if flags contains the specific user flag. This method is more efficient compared to 'HasFlag()'.
    /// </summary>
    /// <param name="haystack">The bunch of flags</param>
    /// <param name="needle">The flag to look for.</param>
    /// <returns>Return true if flag found in flags.</returns>
    public static bool Contains(this UserFlags haystack, UserFlags needle)
    {
        return (haystack & needle) == needle;
    }
}

[Flags]
public enum UserFlags
{
    Script = 1,                                     // 0x1
    AccountDisabled = 2,                            // 0x2
    HomeDirectoryRequired = 8,                      // 0x8
    AccountLockedOut = 16,                          // 0x10
    PasswordNotRequired = 32,                       // 0x20
    PasswordCannotChange = 64,                      // 0x40
    EncryptedTextPasswordAllowed = 128,             // 0x80
    TempDuplicateAccount = 256,                     // 0x100
    NormalAccount = 512,                            // 0x200
    InterDomainTrustAccount = 2048,                 // 0x800
    WorkstationTrustAccount = 4096,                 // 0x1000
    ServerTrustAccount = 8192,                      // 0x2000
    PasswordDoesNotExpire = 65536,                  // 0x10000 (Also 66048 )
    MnsLogonAccount = 131072,                       // 0x20000
    SmartCardRequired = 262144,                     // 0x40000
    TrustedForDelegation = 524288,                  // 0x80000
    AccountNotDelegated = 1048576,                  // 0x100000
    UseDesKeyOnly = 2097152,                        // 0x200000
    DontRequirePreauth = 4194304,                   // 0x400000
    PasswordExpired = 8388608,                      // 0x800000 (Applicable only in Window 2000 and Window Server 2003)
    TrustedToAuthenticateForDelegation = 16777216,  // 0x1000000
    NoAuthDataRequired = 33554432                   // 0x2000000
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

查找 AD 中是否启用或禁用用户帐户 的相关文章

随机推荐

  • 从 URL 缓存并保存 WebView 中的所有图像内容并加载

    我有在 Android 中运行的 Web 应用程序 我可以缓存我的网络 因此如果用户没有互联网连接 他仍然可以从缓存访问网络 但它仅在用户没有互联网连接时运行 现在 为了优化我的应用程序 当用户有互联网连接时 我想缓存所有显示的图像WebV
  • fflush 和 fsync 之间的区别

    我想fsync does fflush 在内部 所以使用fsync 在流上就可以了 但在网络 I O 下执行时我得到了意想不到的结果 我的代码片段 FILE fp fopen file wb multiple fputs calls lik
  • 如何创建书签以覆盖外部文件中的 html/div 层和 CSS

    我正在尝试找到一种方法来创建一个书签 它将 从外部文件 加载一个带有其他 html 和 css 的新图层 div 并将其覆盖在现有页面上 有人有可以分享的书签示例吗 我可以使用新的 html 内容和 CSS 类创建 div 我只是不确定如何
  • 从 Oracle Forms 中的 DLL (c#) 调用函数

    我在 Oracle Forms 6i 中从 DLL 调用方法时遇到了大问题 DLL已写入 C 代码如下 using System using System Collections Generic using System Linq usin
  • 我可以检查哪些函数模板已经或尚未实例化至少一次吗?

    我有很多模板代码 由于错误的模板代码除非经过编译 否则不会引发编译器错误 因此有什么方法可以检查编译器实际 编译 的模板函数以及完全忽略的模板函数吗 EDIT 2 如果一个特定的类模板 or 函数模板实例化一次 对于任何参数类型 都可以 我
  • Meteor's blaze 和 Famo.us 如何一起玩?

    2 技术 使用 blaze 模板引擎的 Meteor Famo us 及其出色的 GUI 框架 我来自流星方面 我个人喜欢使用 mustache 把手 从数据驱动 gui 反应式会话 数据库使得这非常高效和直观 现在famo us 及其所有
  • 使 FAB 不被夹在底部导航栏内

    我正在尝试将底部导航栏与 FAB 重叠 我希望我的导航栏看起来像这样 但它却像这样切断了按钮 如何防止FAB被切断 这是我的 XML
  • Twitterizer 2 和 C# - 找不到命名空间

    我对 Twitterizer2 有一个愚蠢的问题 可能还有我 我通过右键单击引用并浏览找到它们 从我的下载目录中添加了引用 twitterizer 2 3 1 以及 newtonsoft 然后我添加 using Twitterizer 你瞧
  • JQuery Mobile,加载表单提交页面时出现问题

    我正在开发我们网站的移动视图 我正在尝试实现 JQuery 移动版 但我发现两个问题可能会阻止我继续 并希望你们中的一些人有见解 许多页面都是通过搜索打开的 但是 当从搜索框调出页面时 我无法将其加载为 rel external 因此页面通
  • 在 C#.NET 中使用 USB PS2 手控器

    我正在尝试创建一个程序 该程序从 USB PS2 手持控制器获取输入 转换信息并将其传递到 RS232 设备 我已经完成了 rs232 设备的所有工作 问题出在与 USB 控制器的接口上 似乎没有任何好的文档 而且 NET3 0 3 5 也
  • AppCompat 23.3 支持向量不再起作用?

    我正在使用添加的支持向量绘图支持库23 2 http android developers blogspot com 2016 02 android support library 232 html与 AppCompat 一起 我正在使用矢
  • C中的getchar()无需按Enter键即可完成

    From my 上一篇文章 https stackoverflow com q 27297811 3429430 我知道 getchar 仅当我们按 Enter 时才完成 让我们考虑一下这段代码 include
  • GIThub API 文件上传问题

    I am trying to upload file to GitHub via GITHub API http developer github com v3 repos contents http developer github co
  • Python:从线程调用时解密失败或记录错误

    我在此代码片段中收到 解密失败或坏记录 mac 错误 conn psycopg2 connect cursor conn cursor cursor execute SELECT id ip FROM schema table rows c
  • Maven 找不到要运行的 JUnit 测试

    我有一个 Maven 程序 它编译得很好 当我跑步时mvn test它不运行任何测试 在 TESTs 标题下说There are no tests to run 我用一个超级简单的设置重新创建了这个问题 我将在下面包含该设置以及运行时的输出
  • 是否可以在 ASP.NET MVC 3 中显示数据库中的原始 Html?

    我的数据库中有一个表 其中一个属性是 Html 页面 没有 html head 和 body 标签 我打算将它放在我的一个视图的中间 比如说 我调用一个 cotroller 方法接受一个参数 并返回一个视图 传递这个 html 大字符串作为
  • 如何更改android中选项卡指示器文本的颜色?

    如何更改选项卡文本指示器的颜色 我可以使用引用的选择器标签更改图标example http developer android com resources tutorials views hello tabwidget html 但不能改变
  • 如何使用 Spring Data / JPA 插入 Postgres Array 类型列?

    假设我有一个像这样的 postgres 表 CREATE TABLE sal emp name text pay by quarter integer schedule text 我什至可以使用 Spring Data 插入列中pay by
  • CertManager Letsencrypt CertificateRequest“无法执行自检 GET 请求”

    Waiting for http 01 challenge propagation failed to perform self check GET request 和这个bug类似https github com jetstack cer
  • 查找 AD 中是否启用或禁用用户帐户

    我需要查找在 AD 中是否启用或禁用用户帐户 我找不到旗帜或财产 用户帐户控制 这可以使用 USERPRINCIPAL 类来实现吗 drop persona1 Items Clear string valor drop area Selec