簡體   English   中英

如何在 Active Directory 中了解用戶密碼是否過期?

[英]How to learn whether a user password is expired or not in Active Directory?

在我的 ASP.NET MVC (C#) 項目中,我必須了解用戶密碼是否過期? 我在互聯網上找到了一些關於此的答案,但它們對我沒有用。

第一種方法是使用maxpwdage + pwdlastset = password expired date ,第二種方法是使用useraccountcontrol屬性來了解它是否過期。 如果此屬性的值為 8389120,則用戶密碼已過期。

雖然 AD 中的用戶密碼已過期,但useraccountcontrol值仍為 512。我嘗試使用maxpwdage + pwdlastset但我看不到maxpwdage類的屬性(我讓用戶成為管理員)

Active Directory 用戶密碼到期日期 .NET/OU 組策略(第一種方式) https://support.microsoft.com/en-us/kb/305144 (第二種方式)

由於我上面提到的原因,它們都不起作用。

有沒有其他方法可以做到這一點,或者我如何查看maxpwdage屬性的值?

編輯:我從這里得到我想要的用戶

            DirectoryEntry dEntry = new DirectoryEntry
                        ( "LDAP://a.b.c:123/OU=d, DC=e, DC=f", this.GetAdUserName(),
                        this.GetAdUserPassword() );
            DirectorySearcher directorySearcher = new DirectorySearcher( dEntry );
            directorySearcher.Asynchronous = true;
            directorySearcher.CacheResults = true;
            directorySearcher.Filter = "(&(sAMaccountName=" + identificationNumber + "))";
            SearchResult user = directorySearcher.FindOne();
            return user;

我正在檢查用戶的屬性,但找不到maxpwdage屬性。

您可以使用代表時間間隔的TimeSpan 然后您只需要檢查今天的日期和過期日期。

DateTime expireDate = passwordLastChanged.AddDays(iMaxPwdAge);
TimeSpan ts = expireDate - DateTime.Now;
int iDaysTilExpired = ts.Days;
WriteLogMessage("Days til password expires:" + iDaysTilExpired);

還有一個很好的例子,我為我的項目更改了一些部分,它對我有用。

編輯 :

您可以使用屬性msDS-UserPasswordExpiryTimeComputed來獲取用戶密碼到期日期。

並且

'maxPwdAge' 屬性保存在 domainDNS 類(目錄的根)上,因為它是策略的一部分。 它不保存在用戶對象上。 如果您使用的是 .NET 2.0,則可以輕松獲得:

using (DirectoryEntry domain = Domain.GetCurrentDomain())
{
    DirectorySearcher ds = new DirectorySearcher(
        domain,
        "(objectClass=*)",
        null,
        SearchScope.Base
        );

        SearchResult sr = ds.FindOne();

        TimeSpan maxPwdAge = TimeSpan.MinValue;

        if (sr.Properties.Contains("maxPwdAge"))
            maxPwdAge = TimeSpan.FromTicks((long)sr.Properties["maxPwdAge"][0]);
}

編輯 2:

這是您可以使用的完整示例:

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();
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM