我已经搜索了网站以获取信息,并找到了以下信息:ASP.NET C#Active Directory-请参阅用户密码到期多久以来

这说明了如何根据域策略获得密码何时到期的价值。

我的问题是:如果用户具有具有不同的MaxPasswordage值的OU组策略,那么在域组策略中指定的一个策略覆盖了一个策略,该怎么办?如何以编程方式获取OU的组策略对象?

编辑: 为了使这个问题更加清楚,我正在添加此编辑。我之所以追求的是能够判断用户的密码何时到期。据我了解,日期值可以由本地策略或组对象策略支配。我有一个将LINQ转换为LDAP查询的LINQ2DirectoryService提供商。因此,获取日期到期值的LDAP查询对于此subj将是最佳的。如果您回答包括.NET支持的哪些对象包装器包含在此方程式中 - 那将是一个死亡的答案!

有帮助吗?

解决方案

让我从 http://support.microsoft.com/kb/323750 其中包含Visual Basic和vbscript示例以及 http://www.anitkb.com/2010/03/how-to-implement-active-directory.html 概述了MAXPWDAGE OU设置如何影响计算机,而不是用户。它也有评论指向 aloinfo.exe 作为MS的工具,可用于获取密码年龄。

这是一个例子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;

namespace LDAP
{
    class Program
    {
        static void Main(string[] args)
        {
            string domainAndUsername = string.Empty;
            string domain = string.Empty;
            string userName = string.Empty;
            string passWord = string.Empty;
            AuthenticationTypes at = AuthenticationTypes.Anonymous;
            StringBuilder sb = new StringBuilder();

            domain = @"LDAP://w.x.y.z";
            domainAndUsername = @"LDAP://w.x.y.z/cn=Lawrence E."+
                        " Smithmier\, Jr.,cn=Users,dc=corp,"+
                        "dc=productiveedge,dc=com";
            userName = "Administrator";
            passWord = "xxxpasswordxxx";
            at = AuthenticationTypes.Secure;

            DirectoryEntry entry = new DirectoryEntry(
                        domain, userName, passWord, at);

            DirectorySearcher mySearcher = new DirectorySearcher(entry);

            SearchResultCollection results;
            string filter = "maxPwdAge=*";
            mySearcher.Filter = filter;

            results = mySearcher.FindAll();
            long maxDays = 0;
            if(results.Count>=1)
            {
                Int64 maxPwdAge=(Int64)results[0].Properties["maxPwdAge"][0];
                maxDays = maxPwdAge/-864000000000;
            }

            DirectoryEntry entryUser = new DirectoryEntry(
                        domainAndUsername, userName, passWord, at);
            mySearcher = new DirectorySearcher(entryUser);

            results = mySearcher.FindAll();
            long daysLeft=0;
            if (results.Count >= 1)
            {
                var lastChanged = results[0].Properties["pwdLastSet"][0];
                daysLeft = maxDays - DateTime.Today.Subtract(
                        DateTime.FromFileTime((long)lastChanged)).Days;
            }
            Console.WriteLine(
                        String.Format("You must change your password within"+
                                      " {0} days"
                                     , daysLeft));
            Console.ReadLine();
        }
    }
}

其他提示

以下代码对我有用,以获取域和本地用户帐户上的密码到期日期:

public static DateTime GetPasswordExpirationDate(string userId, string domainOrMachineName)
{
    using (var userEntry = new DirectoryEntry("WinNT://" + domainOrMachineName + '/' + userId + ",user"))
    {
        return (DateTime)userEntry.InvokeGet("PasswordExpirationDate");
    }
}

使用以下方法获取帐户的到期日期 -

public static DateTime GetPasswordExpirationDate(string userId)
    {
        string forestGc = String.Format("GC://{0}", Forest.GetCurrentForest().Name);
        var searcher = new DirectorySearcher();
        searcher = new DirectorySearcher(new DirectoryEntry(forestGc));
        searcher.Filter = "(sAMAccountName=" + userId + ")";
        var results = searcher.FindOne().GetDirectoryEntry();
        return (DateTime)results.InvokeGet("PasswordExpirationDate");
    }

以前的某些答案依赖于DirectoryEntry.invokeget方法, MS说不应使用哪个. 。因此,这是另一种方法:

public static DateTime GetPasswordExpirationDate(UserPrincipal user)
{
    DirectoryEntry deUser = (DirectoryEntry)user.GetUnderlyingObject();
    ActiveDs.IADsUser nativeDeUser = (ActiveDs.IADsUser)deUser.NativeObject;
    return nativeDeUser.PasswordExpirationDate;
}

您需要添加通常在C: Windows System32 activeds.tlb上找到的Activeds com库的引用。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top