تاريخ انتهاء كلمة مرور المستخدم Active Directory .NET/OU سياسة المجموعة

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

  •  04-10-2019
  •  | 
  •  

سؤال

لقد بحثت في الموقع للحصول على معلومات ووجدت هذا:ASP.NET C# Active Directory - انظر كم من الوقت قبل انتهاء صلاحية كلمة مرور المستخدم

وهو ما يفسر كيفية الحصول على قيمة عندما تنتهي صلاحية كلمة المرور وفقًا لسياسة المجال.

سؤالي هو: ماذا لو كان لدى المستخدم سياسة مجموعة OU التي لها قيمة مختلفة من MaxPasswordage ، مما يتجاوز تلك المحددة في سياسة مجموعة المجال؟ كيفية الحصول على كائن سياسة مجموعة OU بشكل برمجي؟

يحرر: لجعل هذا السؤال أكثر وضوحًا قليلاً ، أقوم بإضافة هذا التحرير. ما أقوم به هو أن أتمكن من معرفة متى تنتهي صلاحية كلمة مرور المستخدم. بقدر ما أفهم أن قيمة التاريخ يمكن أن تحكمها السياسة المحلية للمجالات أو سياسة كائن المجموعة. لدي مزود LINQ2DirectoryService يترجم LINQ إلى استعلامات LDAP. لذا فإن استعلام 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");
    }

تعتمد بعض الإجابات السابقة على طريقة directory.invokeget ، ما هو لا ينبغي استخدام MS. إذن هنا نهج آخر:

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

ستحتاج إلى إضافة إشارة إلى مكتبة COM ACTIVESS الموجودة عادة في C: Windows System32 Activeds.tlb.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top