{"id":37,"date":"2009-02-10T23:16:00","date_gmt":"2009-02-10T22:16:00","guid":{"rendered":"\/post\/2009\/02\/10\/User-Search-in-AD.aspx"},"modified":"2014-02-22T19:05:02","modified_gmt":"2014-02-22T18:05:02","slug":"user-search-in-ad","status":"publish","type":"post","link":"https:\/\/cymbeline.ch\/2009\/02\/10\/user-search-in-ad\/","title":{"rendered":"User Search in AD"},"content":{"rendered":"

\nI stumbled upon the System.DirectoryServices.AccountManagement namespace this week. It was introduced with .Net 3.5 and offers functionality to perform queries on AD objects like users, groups and computers in a more comfortable way than through the DirectorySearcher class from the System.DirectoryServices namespace. To illustrate the ease of using these classes, I came up with a tiny example which lists all users whose account name (the samAccountName attribute in AD) starts with an 'a'. On top of this, using LINQ it is quite simple to convert the resulting PrincipalSearchResult<Principal> collection into an IEnumerable<UserPrincipal><\/em>.\n<\/p>\n

\r\nusing System;\r\nusing System.Collections.Generic;\r\nusing System.DirectoryServices.AccountManagement;\r\nusing System.Linq;\r\n\r\nnamespace UserSearch\r\n{\r\n    class Program\r\n    {\r\n        static void Main(string[] args)\r\n        {\r\n            PrincipalContext context = new PrincipalContext(ContextType.Domain, "contoso.com");\r\n\r\n            UserPrincipal searchFilter = new UserPrincipal(context);\r\n            searchFilter.SamAccountName = "a*";\r\n\r\n            PrincipalSearcher ps = new PrincipalSearcher(searchFilter);\r\n\r\n            IEnumerable<UserPrincipal> results = from principal in ps.FindAll()\r\n                                                 where principal is UserPrincipal\r\n                                                 select principal as UserPrincipal;\r\n\r\n            foreach (UserPrincipal user in results)\r\n            {\r\n                Console.WriteLine("User '{0}' ({1}) Info:", user.SamAccountName, user.Name);\r\n                Console.WriteLine("    Password Set On  {0}", user.LastPasswordSet);\r\n                Console.WriteLine("    Last Log On      {0}", user.LastLogon);\r\n                Console.WriteLine();\r\n            }\r\n        }\r\n    }\r\n}\r\n<\/pre>\n

<\/p>","protected":false},"excerpt":{"rendered":"

I stumbled upon the System.DirectoryServices.AccountManagement namespace this week. It was introduced with .Net 3.5 and offers functionality to perform queries on AD objects like users, groups and computers in a more comfortable way than through the DirectorySearcher class from the System.DirectoryServices namespace. To illustrate the ease of using these classes, I came up with a … Continue reading “User Search in AD”<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[61],"tags":[16,37,17,12,26],"yoast_head":"\nUser Search in AD - Tales of a Code Monkey<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/cymbeline.ch\/2009\/02\/10\/user-search-in-ad\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"User Search in AD - Tales of a Code Monkey\" \/>\n<meta property=\"og:description\" content=\"I stumbled upon the System.DirectoryServices.AccountManagement namespace this week. It was introduced with .Net 3.5 and offers functionality to perform queries on AD objects like users, groups and computers in a more comfortable way than through the DirectorySearcher class from the System.DirectoryServices namespace. To illustrate the ease of using these classes, I came up with a … Continue reading "User Search in AD"\" \/>\n<meta property=\"og:url\" content=\"https:\/\/cymbeline.ch\/2009\/02\/10\/user-search-in-ad\/\" \/>\n<meta property=\"og:site_name\" content=\"Tales of a Code Monkey\" \/>\n<meta property=\"article:published_time\" content=\"2009-02-10T22:16:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2014-02-22T18:05:02+00:00\" \/>\n<meta name=\"author\" content=\"roger\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"roger\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/cymbeline.ch\/2009\/02\/10\/user-search-in-ad\/\",\"url\":\"https:\/\/cymbeline.ch\/2009\/02\/10\/user-search-in-ad\/\",\"name\":\"User Search in AD - Tales of a Code Monkey\",\"isPartOf\":{\"@id\":\"https:\/\/cymbeline.ch\/#website\"},\"datePublished\":\"2009-02-10T22:16:00+00:00\",\"dateModified\":\"2014-02-22T18:05:02+00:00\",\"author\":{\"@id\":\"https:\/\/cymbeline.ch\/#\/schema\/person\/cf1f3b3205f4266bf8b68a80fa6b0916\"},\"breadcrumb\":{\"@id\":\"https:\/\/cymbeline.ch\/2009\/02\/10\/user-search-in-ad\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/cymbeline.ch\/2009\/02\/10\/user-search-in-ad\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/cymbeline.ch\/2009\/02\/10\/user-search-in-ad\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/cymbeline.ch\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"User Search in AD\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/cymbeline.ch\/#website\",\"url\":\"https:\/\/cymbeline.ch\/\",\"name\":\"Tales of a Code Monkey\",\"description\":\"... the adventures of a guy making software.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/cymbeline.ch\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/cymbeline.ch\/#\/schema\/person\/cf1f3b3205f4266bf8b68a80fa6b0916\",\"name\":\"roger\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/cymbeline.ch\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/33cc08db99ae52beb26ab6fda46e0eb7?s=96&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/33cc08db99ae52beb26ab6fda46e0eb7?s=96&r=g\",\"caption\":\"roger\"},\"sameAs\":[\"https:\/\/www.cymbeline.ch\"],\"url\":\"https:\/\/cymbeline.ch\/author\/roger\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"User Search in AD - Tales of a Code Monkey","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/cymbeline.ch\/2009\/02\/10\/user-search-in-ad\/","og_locale":"en_US","og_type":"article","og_title":"User Search in AD - Tales of a Code Monkey","og_description":"I stumbled upon the System.DirectoryServices.AccountManagement namespace this week. It was introduced with .Net 3.5 and offers functionality to perform queries on AD objects like users, groups and computers in a more comfortable way than through the DirectorySearcher class from the System.DirectoryServices namespace. To illustrate the ease of using these classes, I came up with a … Continue reading \"User Search in AD\"","og_url":"https:\/\/cymbeline.ch\/2009\/02\/10\/user-search-in-ad\/","og_site_name":"Tales of a Code Monkey","article_published_time":"2009-02-10T22:16:00+00:00","article_modified_time":"2014-02-22T18:05:02+00:00","author":"roger","twitter_misc":{"Written by":"roger","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/cymbeline.ch\/2009\/02\/10\/user-search-in-ad\/","url":"https:\/\/cymbeline.ch\/2009\/02\/10\/user-search-in-ad\/","name":"User Search in AD - Tales of a Code Monkey","isPartOf":{"@id":"https:\/\/cymbeline.ch\/#website"},"datePublished":"2009-02-10T22:16:00+00:00","dateModified":"2014-02-22T18:05:02+00:00","author":{"@id":"https:\/\/cymbeline.ch\/#\/schema\/person\/cf1f3b3205f4266bf8b68a80fa6b0916"},"breadcrumb":{"@id":"https:\/\/cymbeline.ch\/2009\/02\/10\/user-search-in-ad\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/cymbeline.ch\/2009\/02\/10\/user-search-in-ad\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/cymbeline.ch\/2009\/02\/10\/user-search-in-ad\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/cymbeline.ch\/"},{"@type":"ListItem","position":2,"name":"User Search in AD"}]},{"@type":"WebSite","@id":"https:\/\/cymbeline.ch\/#website","url":"https:\/\/cymbeline.ch\/","name":"Tales of a Code Monkey","description":"... the adventures of a guy making software.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/cymbeline.ch\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/cymbeline.ch\/#\/schema\/person\/cf1f3b3205f4266bf8b68a80fa6b0916","name":"roger","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cymbeline.ch\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/33cc08db99ae52beb26ab6fda46e0eb7?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/33cc08db99ae52beb26ab6fda46e0eb7?s=96&r=g","caption":"roger"},"sameAs":["https:\/\/www.cymbeline.ch"],"url":"https:\/\/cymbeline.ch\/author\/roger\/"}]}},"_links":{"self":[{"href":"https:\/\/cymbeline.ch\/wp-json\/wp\/v2\/posts\/37"}],"collection":[{"href":"https:\/\/cymbeline.ch\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cymbeline.ch\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cymbeline.ch\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cymbeline.ch\/wp-json\/wp\/v2\/comments?post=37"}],"version-history":[{"count":5,"href":"https:\/\/cymbeline.ch\/wp-json\/wp\/v2\/posts\/37\/revisions"}],"predecessor-version":[{"id":114,"href":"https:\/\/cymbeline.ch\/wp-json\/wp\/v2\/posts\/37\/revisions\/114"}],"wp:attachment":[{"href":"https:\/\/cymbeline.ch\/wp-json\/wp\/v2\/media?parent=37"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cymbeline.ch\/wp-json\/wp\/v2\/categories?post=37"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cymbeline.ch\/wp-json\/wp\/v2\/tags?post=37"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}