views:

15

answers:

1

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.

A: 

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();
MikeWyatt