【问题标题】:C# ASP.NET - Search AD for active and 'suspended' usersC# ASP.NET - 在 AD 中搜索活动和“暂停”用户
【发布时间】:2016-03-23 10:28:28
【问题描述】:

我有一个脚本,当用户请求新用户时将执行该脚本。

此脚本将获取用户提供的用户名,并搜索 AD 以查看其是否存在。这绝对没问题,但是我们的 IT 部门在帐户上有到期日期。这会导致帐户处于某种“暂停”状态,而不是被禁用,直到它被移至已离职员工的单独 OU。

C# AD 搜索忽略这些暂停的帐户。

以前有人遇到过这个问题吗?或者有没有人知道如何在搜索中适应这些用户?

public static string ADSearch(string ADPart, string Alias)
{
    System.DirectoryServices.DirectoryEntry dirEntry = default(System.DirectoryServices.DirectoryEntry);
    System.DirectoryServices.DirectorySearcher dirSearcher = default(System.DirectoryServices.DirectorySearcher);
    try
    {
        dirEntry = new System.DirectoryServices.DirectoryEntry("LDAP://LDAP DETAILS HERE");

        dirSearcher = new System.DirectoryServices.DirectorySearcher(dirEntry);
        dirSearcher.Filter = "(samaccountname=" + Alias + ")";

        dirSearcher.PropertiesToLoad.Add("GivenName");
        //Users first name
        dirSearcher.PropertiesToLoad.Add("sn");
        //Users last name
        dirSearcher.PropertiesToLoad.Add("mail");
        //Users e-mail
        dirSearcher.PropertiesToLoad.Add("samaccountname");
        //Samaccount
        StringBuilder groupNames = new StringBuilder(); //stuff them in | delimited


        SearchResult sr = dirSearcher.FindOne();
        //return false if user isn't found


        if (sr != null)
            if (ADPart == "GivenName")
                return sr.Properties["GivenName"][0].ToString().Replace("'", "");
            else if (ADPart == "sn")
                return sr.Properties["sn"][0].ToString().Replace("'", "");
            else if (ADPart == "mail")
                return sr.Properties["mail"][0].ToString().Replace("'", "");
            else if (ADPart == "alias")
                return sr.Properties["samaccountname"][0].ToString().Replace("'", "");
            else
                return null;
        else
            return null;

        // return false if exception occurs
    }
    catch (Exception ex)
    {
        return ex.Message;
    }
}

可能值得注意的是,这段代码不是我写的,它已经到位了。

非常感谢任何帮助。

【问题讨论】:

    标签: c# asp.net active-directory directoryservices


    【解决方案1】:

    这里有一些代码应该适合你。

    首先,将“accountExpires”添加到PropertiesToLoad,然后可以用这个来查看账号是否过期:

    var isExpired = false;
    if (sr.Properties.Contains("accountExpires")) {
        var expiry = (long)sr.Properties["accountExpires"][0];
        if (!expiry.Equals(9223372036854775807) && !expiry.Equals(0)) {
            isExpired = DateTime.FromFileTime(expiry) <= DateTime.Now;
        }
    }
    

    神奇的数字是因为,正如the documentation states,“0 或 0x7FFFFFFFFFFFFFFFF (9223372036854775807) 的值表示帐户永不过期。”

    【讨论】:

    • 嗨加布里埃尔,非常感谢您的详细回复。你当然知道你在说什么。不幸的是,这不符合我的需求,因为查询根本没有看到任何过期的帐户,所以它不能计算它们。再次感谢!
    • 此特定搜索一次仅搜索一个帐户,基于:“(samaccountname=" + Alias + ")”。那么是什么将帐户名称提供给此方法?
    • 有一个文本框,管理员可以在其中输入用户名。他们输入的任何内容都会在搜索中使用。
    • 那么你是说如果他们输入一个过期帐户的用户名,那么它什么也找不到?
    • 嗯,我刚刚和技术人员交谈过,结果发现整个问题都是人为错误。 AD 搜索实际上运行良好。快乐的时光。非常感谢您的帮助加布里埃尔。我会投票赞成你的回答,因为它确实很有教育意义。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-13
    • 2011-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多