【问题标题】:How to retrieve the creation date of an AD user from .NET?如何从 .NET 中检索 AD 用户的创建日期?
【发布时间】:2010-02-09 10:59:26
【问题描述】:

我想检索 Active Directory 中用户的创建日期。我知道用户有一个 WhenCreated 属性,但我看不到该属性暴露在我正在使用的 UserPrincipal 类型上。

我想做这样的事情:

var principal = UserPrincipal.FindByIdentity(context, IdentityType.Guid, guid);
//var createdDate = principal.CreationDate; 

编辑:这是在 IIS 中运行的 Web 应用程序,查询同一网络上另一台计算机上的 AD 服务器。

【问题讨论】:

  • 这是桌面应用还是浏览器应用?
  • @Keith - 它是一个 asp.net mvc 网络应用程序

标签: .net active-directory


【解决方案1】:

您需要获取底层的 DirectoryEntry(System.DirectoryServices-namespace 的一部分)。

您可以使用此扩展方法以字符串形式获取日期:

public static String GetProperty(this Principal principal, String property)
{
    DirectoryEntry directoryEntry = principal.GetUnderlyingObject() as DirectoryEntry;
    if (directoryEntry.Properties.Contains(property))
        return directoryEntry.Properties[property].Value.ToString();
    else
        return String.Empty;
}

这样使用:

var createdDate = principal.GetProperty("whenCreated");

【讨论】:

    猜你喜欢
    • 2016-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-08
    • 2021-12-30
    • 2020-02-07
    • 1970-01-01
    • 2016-08-01
    相关资源
    最近更新 更多