【问题标题】:Unable to get Changes Using uSNChanged无法使用 uSNChanged 获取更改
【发布时间】:2022-01-22 23:53:17
【问题描述】:

我想使用 C# 从 Active Directory 获取增量更改。为此,我正在尝试构建以下文章中提到的解决方案。

https://docs.microsoft.com/en-us/windows/win32/ad/polling-for-changes-using-usnchanged

但是,我面临以下问题:

  1. 没有可供用户使用的 uSNChanged 属性(尽管它可用于 OU,即组织单位)。我只能看到用户的以下属性。

  1. 当我将用户从 OU1 移动到 OU2 时,highestcommittedusn 会增加,但是当我使用以下代码查询更改 (search.Filter = "(uSNChanged>=13000)") 时,我不会得到任何更新。

  2. 在 OU 中添加用户也是如此。

示例代码如下:

public static void GetUpdates()
{
    var myLdapConnection = createDirectoryEntry();
    var search = new DirectorySearcher(myLdapConnection);
    search.Filter = "(uSNChanged>=13000)";
    search.SearchScope = System.DirectoryServices.SearchScope.Subtree;
    var results = search.FindAll();
    Console.WriteLine(results.Count);
}

public static DirectoryEntry createDirectoryEntry()
{
    DirectoryEntry ldapConnection = new DirectoryEntry("LDAP://adfs.fed.abcd.com/DC=adfs,DC=fed,DC=abcd,DC=com");
    ldapConnection.Path = "adfs.fed.abcd.com";
    ldapConnection.AuthenticationType = AuthenticationTypes.Secure;
            return ldapConnection;
}

 private static long GetHighestUsn()
 {
     using (LdapConnection connection = new LdapConnection(ldapPath))
     {
          var filter = "(&(objectClass=*))";
          var searchRequest = new SearchRequest(null, filter, System.DirectoryServices.Protocols.SearchScope.Base, "highestCommittedUSN");
          var response = connection.SendRequest(searchRequest) as SearchResponse;
          var usn = response.Entries[0].Attributes["highestcommittedusn"][0];
          return Convert.ToInt64(usn);
      }
      return 0;
}

非常感谢任何帮助。

编辑:

  1. 我只有一个域控制器。

【问题讨论】:

    标签: c# active-directory ldap directoryservices directorysearcher


    【解决方案1】:

    对于GetHighestUsn,我认为new SearchRequest(null ... 中的null 应替换为空字符串以获取RootDSE。

    其次你混合System.DirectoryServicesSystem.DirectoryServices.Protocols:你最好坚持一个。目前尚不清楚new LdapConnection(ldapPath))ldapPath 的值是多少。如果是“adfs.fed.abcd.com”并且你有多个域控制器,你无法确切知道哪个会回答,这与最后一句话有关。

    最后,USNChanged 属性是不可复制的属性,这意味着您应该始终请求同一个域控制器来获取更新。另一个域控制器将为同一个对象存储一个完全不同的值。

    通常每个对象都应该返回 USNChanged 属性,除非您指定了要返回的有限属性列表,在这种情况下您也必须包含它。

    编辑:包括示例代码 (Powershell)

    • System.DirectoryServices
        $entry = [System.DirectoryServices.DirectoryEntry]::new("LDAP://localhost:50005/O=MyAppInstance")
        $entry.psbase.AuthenticationType = [System.DirectoryServices.AuthenticationTypes]::Secure
        $searcher = [System.DirectoryServices.DirectorySearcher]::new($entry)
        $searcher.Filter = "(uSNChanged>=13004)"
        $searcher.SearchScope = [System.DirectoryServices.SearchScope]::Subtree
        $results = $searcher.FindAll()
        $results.Count
    
    • System.DirectoryServices.Protocols
        $con = [System.DirectoryServices.Protocols.LdapConnection]::new("localhost:50005")
        $con.AuthType = [System.DirectoryServices.Protocols.AuthType]::Negotiate
        $con.SessionOptions.ProtocolVersion = 3
        $con.Bind()
    
        $req = [System.DirectoryServices.Protocols.SearchRequest]::new()
        $req.DistinguishedName = "O=MyAppInstance"
        $req.Scope = [System.DirectoryServices.Protocols.SearchScope]::Subtree
        $req.Filter = "(uSNChanged>=13004)"
    
        $res = [System.DirectoryServices.Protocols.SearchResponse]$con.SendRequest($req)
        $res.Entries.Count
    

    【讨论】:

    • 感谢@Hazrelle 对此的快速回复。如果我传递空字符串或空字符串,我将获得相同的 HighestUsn 值。并且它的值在每次操作后都会更新(例如,将用户从一个 OU 移动到另一个 OU)。所以,这不是一个问题。是的,ldap 路径是“adfs.fed.abcd.com”。我只有一个域控制器及其本地环境。所以,我将始终连接到同一个域控制器。知道为什么它没有为用户显示 USNChanged 属性,随后在搜索结果中没有捕获到移动/添加?
    • 通常这应该按预期工作。正在分配新用户: - uSNChanged:13005 - uSNCreated:13005 正在分配移动用户: - uSNChanged:13006; - uSN创建:5000;我已经对本地 ADLDS 实例进行了快速检查,这就是我得到的结果。
    • 我已经测试了两个库,并从域控制器上的“highestcommittedusn”(即 13004)的最新已知值中获得了匹配结果(添加和移动)。新的“highestcommittedusn”是 13006。
    • 你能分享你的代码吗?也许我错过了什么......
    • 我也得到了更新的“highestcommittedusn”值。只是我没有通过 C# 中的搜索获得更新的结果
    【解决方案2】:

    答案:代码是正确的。

    我需要向用户(发出请求)提供“读取所有用户信息”权限,然后我开始为每个对象(包括用户)获取 usnchanged 属性。

    下面的文章给出了启用权限的步骤。 https://social.technet.microsoft.com/Forums/en-US/b34f7295-4989-4440-93af-cebd6d66c711/cannot-read-the-usnchanged-attribute-for-some-users-why?forum=winserverDS

    【讨论】:

    • 哈,我想知道这是否不是,但因为它是一个本地实例,所以我跳过了这部分,认为您使用的用户至少具有执行测试的高权限 :) 很好,你发现了。
    • 实际上,我只使用管理员用户,但我仍然需要明确地这样做,不知道为什么..但很高兴它起作用了。
    猜你喜欢
    • 1970-01-01
    • 2010-11-09
    • 1970-01-01
    • 2021-09-03
    • 2015-07-19
    • 1970-01-01
    • 1970-01-01
    • 2020-09-25
    相关资源
    最近更新 更多