3

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.

Jay
  • 53
  • 7

1 Answers1

6

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
  • 7,842
  • 10
  • 50
  • 71
  • Thank you for your solution, it works fine for me except one thing: When I use the * as a wildcard I don't get any results. It turns out that the wildcard is not necessary if you search for an anr property. Despite that it works pretty well. T – Lukas Dec 17 '14 at 14:03
  • Documentation states that you don't need the asterisk for like searches. https://social.technet.microsoft.com/wiki/contents/articles/22653.active-directory-ambiguous-name-resolution.aspx – Andrew Schultz Sep 07 '21 at 03:57