【问题标题】:PrincipalContext & UserPrincipal how to know when password expires?PrincipalContext 和 UserPrincipal 如何知道密码何时过期?
【发布时间】:2011-04-04 07:48:45
【问题描述】:

我有一个具有很多属性的UserPrincipal 对象,但我找不到密码过期日期的属性。

如何做到这一点?

【问题讨论】:

    标签: c# active-directory


    【解决方案1】:

    这是我能想到的最简单的方法...

    using System.DirectoryServices;
    using System.DirectoryServices.AccountManagement;
    using ActiveDs;
    
    //...
    
    PrincipalContext domain = new PrincipalContext(ContextType.Domain);
    UserPrincipal user = UserPrincipal.FindByIdentity(domain, "username");
    DirectoryEntry entry = (DirectoryEntry)user.GetUnderlyingObject();
    IADsUser native = (IADsUser)entry.NativeObject;
    Console.WriteLine(user.GivenName + "'s password will expire on " + native.PasswordExpirationDate);
    


    注意 #1:ActiveDsAdd Reference 对话框的 COM 选项卡上列为 Active DS 类型库

    注意 #2:据我所知,PasswordExpirationDate 是 UTC 时间。

    【讨论】:

    • 很好的答案。谢谢!提示 - 有时,使用 FindByIdentity 非常慢。主要在本地机器上。在这些情况下,最好使用PrincipalSearcher
    • 我通常不使用“Principal”类。我更喜欢 DirectoryEntry 和 DirectorySearcher 对象。在我的测试中,它们往往更快。
    • ActiveD 存在于哪个程序集中?
    • 这可行,但也会在 Visual Studio 的构建输出中显示数十个与 COM 相关的警告(MSB3305 警告),我无法从我这边禁用。
    • 如果 PasswordExpirationDate = 01/01/1970 ??
    猜你喜欢
    • 1970-01-01
    • 2012-12-09
    • 1970-01-01
    • 2017-04-19
    • 1970-01-01
    • 2011-02-22
    • 2016-06-04
    • 2018-11-24
    • 1970-01-01
    相关资源
    最近更新 更多