【问题标题】:extract LastLogon from Active Directory and set it equal to lastlog.txt text box using C#从 Active Directory 中提取 LastLogon 并使用 C# 将其设置为等于 lastlog.txt 文本框
【发布时间】:2014-12-20 18:45:40
【问题描述】:

我有一个名为用户提取的应用程序,顾名思义,该应用程序的目标是通过从列表框中选择用户来提取有关用户的所有可能信息,选择 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 错误。人们建议使用演员,但我是这项技术的新手,非常感谢示例转换代码,非常感谢这个问题的解决方法谢谢你们!!!!!!!!!!!!

【问题讨论】:

  • @DJKRAZE 如果您想知道这个问题实际上与我之前提出的问题相同,我认为问题已得到解决,我几乎不知道如果只是返回第一个用户的 lastLogon 时间列表框,对于被选中的任何用户,文本框具有相同的值,因此任何关于如何使 lastlog 文本框中的文本为每个用户更改的建议。

标签: c# winforms active-directory ldap directoryservices


【解决方案1】:

要将 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

【讨论】:

    【解决方案2】:

    如果从 DirectoryEntry 读取,例如"rs.GetDirectoryEntry().Properties["lastLogon"].Value",LastLogon 属性是一个可以转换为 IADsLargeInteger 的 COM 对象。

    但如果直接从 SearchResult 中读取,例如"rs.Properties["lastLogon"][0]",LastLogon 属性是一个 Integer8。这假定“lastLogon”已添加到 DirectorySearcher 中的 PropertiesToLoad。

    但请注意,LastLogon 仅记录 DC 上的最后一次登录时间,不会复制。 即用户使用 DC2 登录,DC1 上 LastLogon 的值不会更新。

    【讨论】:

      【解决方案3】:

      点击用户名好像可以从AD获取信息。我建议使用最后一种方法,但通过搜索具有用户名的特定用户。

      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";
          }
      }
      

      希望对你有帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-24
        • 2018-04-08
        • 2022-01-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多