简体   繁体   English

如何查询名称中包含美元符号($)的用户的Active Directory?

[英]How to query Active Directory for a user whose name contains a dollar sign ($)?

I have a code snippet that searches for a locally-defined Windows user : 我有一个代码片段,用于搜索本地定义的Windows用户:

using (PrincipalContext context = new PrincipalContext(ContextType.Machine)) {
    using (Principal queryFilter = new UserPrincipal(context) { Name = accountName }) {
        using (PrincipalSearcher searcher = new PrincipalSearcher(queryFilter)) {
            var principal = searcher.FindOne();
            ...
        }
    }
}

The code works fine if accountName contains normal alphanumerics, (eg "testuser"). 如果accountName包含正常的字母数字,(例如“testuser”),代码可以正常工作。 However, if the name contains a dollar sign (eg "test$user"), FindOne() returns nothing. 但是,如果名称包含美元符号(例如“test $ user”),则FindOne()返回任何内容。

I suspect the $ is being interpreted as some kind of metacharacter. 我怀疑$被解释为某种元字符。 Is there a way of preventing this from happening, so that it is interpreted literally? 有没有办法防止这种情况发生,以便按字面解释?

If you're only looking for a single entry - try this: 如果你只想找一个条目 - 试试这个:

UserPrincipal user = UserPrincipal.FindByIdentity(context, "test$user"); 

Maybe that works? 也许这有效吗?

I think this is an example of a leaking abstraction. 我认为这是泄漏抽象的一个例子。 You can't do LDAP searches on the WinNT provider, which is what the Machine ContextType is using. 您无法在WinNT提供程序上执行LDAP搜索,这是Machine ContextType正在使用的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM