2010-07-07 29 views
1

用旧的和新的(er)。 我正在搁置一个旧的vb.net asp.net 2.0“asmx”服务,以支持一个闪亮的新c#.net asp.net 4.0 WCF服务。在.NET 3.5 DirectoryServices.AccountManagement中等效的模糊名称解析(anr = * ma *)

我的旧服务使用System.DirectoryServices.DirectorySearcher以anr =筛选器效果良好,并允许从单个输入字段进行Google风格搜索用户对象。

我真的想利用3.5的System.DirectoryServices.AccountManagement,但只能够找到微软的“通过实例查询”的变化:

UserPrincipal u = new UserPrincipal(ctx); 
u.GivenName = "Jim"; 
u.Surname = "Daly"; 
PrincipalSearcher ps = new PrincipalSearcher(); 
ps.QueryFilter = u; 
PrincipalSearchResult<Principal> results = ps.FindAll(); 

我的问题是,我必须去除我的DirectorySearcher代码以查找类型或搜索AccountManagement命名空间中是否存在明显的模糊搜索功能?

非常感谢。

J.

回答

5

你也许可以写自己的实现UserPrincipal的暴露的自定义属性:

[DirectoryObjectClass("user")] 
[DirectoryRdnPrefix("CN")] 
public class CustomUserPrincipal : UserPrincipal 
{ 
    public CustomUserPrincipal (PrincipalContext context) : base (context) 
    { 
    } 

    [DirectoryProperty("anr")] 
    public string Anr 
    { 
     get { return (string)ExtensionGet ("anr")[0]; } 
     set { ExtensionSet ("anr", value); } 
    } 
} 

使用

var u = new CustomUserPrincipal(ctx) { Anr = "*mr*" }; 
var ps = new PrincipalSearcher() { QueryFilter = u }; 
var results = ps.FindAll(); 
+0

谢谢您的解决方案,它除了一件事以外,对我来说工作得很好:当我使用*作为通配符时,我没有得到任何结果。事实证明,如果您搜索anr属性,则通配符不是必需的。尽管它工作得很好。 Ť – Lukas 2014-12-17 14:03:31