Active Directoryユーザーパスワード有効期限.NET/OUグループポリシー

StackOverflow https://stackoverflow.com/questions/3764327

  •  04-10-2019
  •  | 
  •  

質問

私はサイトに情報を検索しましたが、これを見つけました:ASP.NET C#Active Directory-ユーザーのパスワードが期限切れになるまでの期間を参照してください

これは、ドメインポリシーに従ってパスワードが期限切れになったときの値を取得する方法を説明しています。

私の質問はこれです:ユーザーが異なるMaxPassWordage値を持つOUグループポリシーを持っている場合、ドメイングループポリシーで指定されたものを最優先している場合はどうなりますか? OUのグループポリシーオブジェクトをプログラム的に取得する方法は?

編集: この質問をもう少し明確にするために、この編集を追加しています。私が望んでいるのは、ユーザーのパスワードがいつ失効するかを知ることができることです。私が理解している限り、日付値は、ドメインのローカルポリシーまたはグループオブジェクトポリシーによって支配される可能性があります。 LINQをLDAPクエリに変換するLINQ2DirectoryServiceプロバイダーがあります。したがって、日付の有効期限の値を取得するLDAPクエリがこのsubjに最適です。回答が含まれている場合、.NETによってサポートされているオブジェクトラッパーがこの方程式に含まれているものが含まれている場合 - それは答えに死んでいるでしょう!

役に立ちましたか?

解決

始めましょう http://support.microsoft.com/kb/323750 視覚的な基本および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