Ambiguous Name Resolution (anr=*ma*) equivalent in .NET 3.5 DirectoryServices.AccountManagement

99封情书 提交于 2019-12-08 04:12:58

问题


Out with the old and in with the new(er). I am shelving an old vb.net asp.net 2.0 "asmx" service in favor of a shiny new c#.net asp.net 4.0 WCF service.

My old service used System.DirectoryServices.DirectorySearcher with an anr= filter to good effect and allowed for a Google style search for user objects from a single input field.

I would really like to take advantage of 3.5's System.DirectoryServices.AccountManagement but have only been able to find variations of Microsoft's "Query by Example":

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

My question is, do I have to dust off my DirectorySearcher code for anr type searches or am I missing some obvious ambiguous search capabilities in the AccountManagement namespace?

Many Thanks.

J.


回答1:


You might be able to write your own implementation of UserPrincipal that exposes a custom property:

[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 ); }
    }
}

Usage

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


来源:https://stackoverflow.com/questions/3195124/ambiguous-name-resolution-anr-ma-equivalent-in-net-3-5-directoryservices-ac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!