簡體   English   中英

使用C#從Active Directory中提取LastLogon並將其設置為等於lastlog.txt文本框

[英]extract LastLogon from Active Directory and set it equal to lastlog.txt text box using C#

我有一個名為“用戶提取”的應用程序,顧名思義,該應用程序的目標是通過僅從列表框中選擇用戶來提取有關用戶的所有可能的信息,samsaccount名稱將被提取並作為值傳遞給txtusersearcher .text文本框,我正在執行SearchUserName.PerformClick(); 我為每個屬性創建了一個文本框,該屬性的值設置為等於該文本框。

我嘗試過的事情

1)

 private void GetUserInformation(string username, string passowrd, string domain)
    {
        Cursor.Current = Cursors.WaitCursor;
        SearchResult rs = null;
        if (txtSearchUser.Text.Trim().IndexOf("@") > 0)
            rs = SearchUserByEmail(GetDirectorySearcher(username, passowrd, domain), txtSearchUser.Text.Trim());
        else
            rs = SearchUserByUserName(GetDirectorySearcher(username, passowrd, domain), txtSearchUser.Text.Trim());

        if (rs != null)
        {
            showuserinfo();
             ShowUserInformation(rs);
        }
        else
        {
            MessageBox.Show("User not found!!!", "Search Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    } 

private void ShowUserInformation(SearchResult rs) {if (rs.GetDirectoryEntry().Properties["lastLogon"].Value != null) lastlog.Text = rs.GetDirectoryEntry().Properties["lastLogon"].Value.ToString();

2)

   if (rs.GetDirectoryEntry().Properties["lastLogon"].Value != null)
          lastlog.Text = (rs.GetDirectoryEntry().Properties["lastLogon"].Value.ToString());
           int  x; 
                x =  Int32.Parse(lastlog.Text);
               lastlog.Text = x.ToString();

3)我在線閱讀了有關使用主體上下文,用戶主體和主體搜索器的技巧,並嘗試了此方法

PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
        UserPrincipal qbeuser = new UserPrincipal(ctx);
        PrincipalSearcher srch = new PrincipalSearcher(qbeuser);
        foreach (var found in srch.FindAll())
        {
            UserPrincipal founduser = found as UserPrincipal;
            {
                if (founduser.LastLogon != null)
                    lastlog.Text = founduser.LastLogon.ToString();

            }
        }

這有效,但這會在列表框中引入第一個用戶的lastlogon,我希望在單擊每個用戶時更改此值。 我知道主要原因是lastlogon值存儲為integer8,而其他則是字符串。 當您使用value.tostring(); 它不起作用,並且出現System.__ComObject錯誤。 人們建議使用強制轉換,但是我對這種技術是陌生的,將非常感謝您提供示例強制轉換代碼,並且非常感謝您解決此問題的解決方法,謝謝大家!

要獲取lastLogon值作為可讀的日期時間屬性,您需要先將其從'ComObject'轉換為long類型,然后將此long值轉換為DateTime:

 DirectoryEntry de = New DirectoryEntry(ldap, logon, password) // get user as DirectoryEntry object
 object adsLargeInteger = de.Properties["lastLogon"].Value;
 if (adsLargeInteger == null)
 {
   // todo 
 }
 long highPart =
            (Int32)
                adsLargeInteger.GetType()
                    .InvokeMember("HighPart", BindingFlags.GetProperty, null, adsLargeInteger, null);
 long lowPart =
            (Int32)
                adsLargeInteger.GetType()
                    .InvokeMember("LowPart", BindingFlags.GetProperty, null, adsLargeInteger, null);
 long lastLogonL = (long)((uint)lowPart + (((long)highPart) << 32)); // Get value as long
 DateTime lastLogonStr = DateTime.FromFileTime(lastLogonL); // get value as DateTime string

似乎您可以在單擊用戶名時從廣告中獲取信息。 我建議使用最后一種方法,但要使用用戶名搜索特定用戶。

private void UpdateLastLogonField(string userName)
{
    PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
    UserPrincipal qbeuser = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, userName);
    if (qbeuser != null && qbeuser.lastlogon != null)
    {
        lastlog.Text = qbeuser.LastLogon.ToString();
    } else {
        lastLog.Text = "Could not get last logon info";
    }
}

希望能幫助到你!

如果從DirectoryEntry中讀取,例如“ rs.GetDirectoryEntry()。Properties [“ lastLogon”]。Value“,則LastLogon屬性是一個COM對象,可以強制轉換為IADsLargeInteger。

但是,如果直接從SearchResult中讀取,例如“ rs.Properties [“ lastLogon”] [0]“,則LastLogon屬性為Integer8。 假定“ lastLogon”已添加到DirectorySearcher的PropertiesToLoad中。

但是請注意,LastLogon僅記錄DC上的上次登錄時間,不會被復制。 即用戶使用DC2登錄,DC1上的LastLogon的值不會更新。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM