使用C#从Active Directory获取信息

时间:2019-11-04 17:41:40

标签: c# active-directory directoryentry userprincipal directorysearcher

我对使用UserPrincipal类(C#)是陌生的,因为我需要帮助来更正此问题以从Active Directory中为特定用户获取某些属性,因此我陷入困境。

private void AD_GetInfo()
{
    DateTime sDT = DateTime.Today;

    PrincipalContext sPContext = new PrincipalContext(ContextType.Domain, "CONTOSA");
    UserPrincipal sUsrPrincipal = new UserPrincipal(sPContext);
    sUsrPrincipal.AdvancedSearchFilter.(sDT, MatchType.Equals(myUserName).ToString);
    PrincipalSearcher sPSearcher = new PrincipalSearcher(sUsrPrincipal);
    UserPrincipal sResult = (UserPrincipal)sPSearcher.FindOne();

    if (sResult != null)
    {
        textbox.text("Display Name                        " + sResult.DisplayName +                "");
        textbox.text("Email Address                       " + sResult.EmailAddress +               "");
        textbox.text("Telephone Number                    " + sResult.VoiceTelephoneNumber +       "");
        textbox.text("Account is Active                   " + sResult.Enabled +                    "");
        textbox.text("Account Expiration Date             " + sResult.AccountExpirationDate +      "");
        textbox.text("Account Lockout Time                " + sResult.AccountLockoutTime +         "");
        textbox.text("Last Bad Password Attempt           " + sResult.LastBadPasswordAttempt +     "");
        textbox.text("Last Logon                          " + sResult.LastLogon +                  "");
        textbox.text("Last Password Set                   " + sResult.LastPasswordSet +            "");
        textbox.text("Password Never Expires              " + sResult.PasswordNeverExpires +       "");
        textbox.text("Bad Logon Count                     " + sResult.BadLogonCount +              "");
    }
}

我尝试使用DirectoryEntryDirectorySearcher,但是遇到accountExpirespwdLastSetlastLogon的问题,因为结果是System.__ComObject,我不知道如何转换。

如果有人可以帮助,请帮助。

谢谢

0 个答案:

没有答案