Académique Documents
Professionnel Documents
Culture Documents
Printing Details URL: http://www.ianatkinson.net/computing/adcsharp.htm Date: 12 Dec 2013 9:45 All content Ian Atkinson 20002013, not to be re-used without permission
System.DirectoryServices.AccountManagement Examples
i. Example 6 - Retrieving Information From a Users Record ii. Example 7 - Retrieving Information From All Users Again iii. Example 8 - Using Both Libraries
Introduction
If you work in the kind of large institution that I do and are using M icrosoft Active Directory then the chances are that at certain times you will need to perform actions on the directory that are outside the scope of the M SAD tools. This could be things like specialised queries, bulk account creation or mass updates of user information. The M SAD tools and even some of the command line tools are quite limiting and difficult to use in this regard. Whatever the reason, you may find that at some point you need to either purchase additional software for managing AD or write your own. Obviously Id rather write my own software as its cheaper, more rewarding and you can customise it however you like! I found that when I was trying to learn how to make C# work nicely with AD there were a lack of simple tutorials to get me started, although I did find a few useful blog posts. Often any examples that I found did much more in the program than I was after, so it was difficult to pick out the few lines that I was actually interested in. So, this page contains a few basic but fully working programs which illustrate common scenarios that you may have. If you can read and understand these examples you should be able to apply the principles to much larger and very powerful programs as I have done. Obviously you need to be careful with this kind of programming and where ever possible you shouldnt be testing on a live environment. Queries are safe enough but when you get on to account creation and modification the potential to royally muck up a lot of account very quickly is a real danger, so take care!
Two Approaches
What is a little bit confusing is that there are essentially two sets of classes which can be used for AD operations. One is easier to use but not as versatile, the other is harder to use but lets you do pretty much anything (within my experience anyway!). Which approach you use will depend on your project requirements. If you literally want to write a password resetting tool or a simple phone book then the A c c o u n t M a n a g e m e n tlibraries probably contain everything you need so you should use those. For anything more complicated you may find that you need to get a bit more down and dirty with the LDAP and use the D i r e c t o r y S e r v i c e sapproach.
System.DirectoryServices
M SDN Documentation The older and more difficult approach is using just S y s t e m . D i r e c t o r y S e r v i c e son its own. This lets you do pretty much anything that you like however the approach is more technical. For example the properties of the AD objects (description, telephone etc.) are all held in an array which can present its own problems and involve a lot of iteration and use of casting since they are all generic objects. Using this approach doing things like setting the password or enabling/disbaling the account is much more cryptic in the way in which it is achieved, often requiring UAC codes to be manually set and so on.
www.ianatkinson.net/computing/adcsharp.htm
1/13
12/12/2013
System.DirectoryServices.AccountManagement
M SDN Documentation The newer approach is to use S y s t e m . D i r e c t o r y S e r v i c e s . A c c o u n t M a n a g e m e n twhich was designed to make managing AD through .NET much easier. Rather than accessing properties using an array they are exposed directly within the classes (and typed accordingly), allowing us to use things like u s e r . D i s p l a y N a m ewhich is much tidier. We also have easy to use methods available such as . S e t P a s s w o r d ( )and . U n l o c k A c c o u n t ( )as well as the . E n a b l e dproperty which can be used to easily manage accounts. These are self explanatory in use once you have retrieved the object from AD so are not included in the examples! The problem as I said though is that whilst the A c c o u n t M a n a g e m e n tlibrary makes things much easier in some regards it is also quite limited in others. It exposes only a small number of the LDAP fields that you may want to use (name, description, email, home dir and phone is about it) so if you need access to a more obscure property it wont suffice.
Mixed Approach
One thing thats worth noting is that you can use the newer libraries to get a U s e r P r i n c i p a lobject as they are called, and then access the underlying LDAP object with the . G e t U n d e r l y i n g O b j e c t ( )method. This means that you could start a program with the newer approach but if you find it too limiting drop into the older approach half way through and have full access. This is not a very neat approach but does work well, we will have a look at how this works in example 8. Hopefully if the newer libraries are expanded further in future .NET released there should be less and less reason to ever need to do this!
System.DirectoryServices Examples
These first examples all use the older approach and will serve you best if you are writing a large or complex AD management program.
www.ianatkinson.net/computing/adcsharp.htm
2/13
12/12/2013
There is no tool that I know of which shows the correlation between the fields in the GUI and what the fields are called in the schema, so it has been necessary for me several times during development to set one of the fields to foo and then run a full query looking for foo in order to reveal the correct field. The example is not too hard to understand, however there are several different classes used in order to accomplish the task. First we create a D i r e c t o r y E n t r yobject. As you will have guessed from the section above regarding your setting, this class will contain all of the information which describes the server we are trying to connect to such as address, username and so on. We then create a D i r e c t o r y S e a r c h e robject. This class describes a search and operates against the D i r e c t o r y E n t r yobject, so it knows where to search, and has its own properties such as its F i l t e rso it knows what to search for. We then use the class S e a r c h R e s u l tagainst the D i r e c t o r y S e a r c h e robject, which represents an LDAP entry. This object has a number of P r o p e r t i e s(such as user name, e-mail address) and a number of generic objects associated with each property: S earchResult result Properties Objects cn mail Ian Atkinson santa@clause.ac.uk users memberof staff domain administrators
The properties have generic objects associated with them as the class has no concept of their content. If you wish you will need to cast or convert to more specific classes in order to perform some operations, for example a telephone extension could be cast to an i n t . In many cases there will be a single object associated with each property, for example a user can have only one user logon name (or s a m a c c o u n t n a m e ). However some properties, such as m e m b e r o fwhich represents a users group membership, will have many objects (one for each group in this case). The S e a r c h R e s u l tobject operates like an array, so we can retrieve a particular value such as r e s u l t . P r o p e r t i e s [ " c n " ] [ 0 ]for the first object associated with the c n property. In the example above r e s u l t . P r o p e r t i e s [ " m e m b e r o f " ] [ 1 ]is "staff". We can also iterate through all of the objects associated with a given property by using the R e s u l t P r o p e r t y C o l l e c t i o nclass, which is what we do in the example below. NB: this first example is more heavily commented than the rest in order to outline the common parts. In subsequent examples I have removed the comments and only commented the new or relevant parts. retrieve_all_info.cs:
view plain
0 1 . 0 2 . 0 3 . 0 4 .
u s i n gS y s t e m ; u s i n gS y s t e m . T e x t ; u s i n gS y s t e m . D i r e c t o r y S e r v i c e s ;
www.ianatkinson.net/computing/adcsharp.htm
3/13
12/12/2013
0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 . 1 5 . 1 6 . 1 7 . 1 8 . 1 9 . 2 0 . 2 1 . 2 2 . 2 3 . 2 4 . 2 5 . 2 6 . 2 7 . 2 8 . 2 9 . 3 0 . 3 1 . 3 2 . 3 3 . 3 4 . 3 5 . 3 6 . 3 7 . 3 8 . 3 9 . 4 0 . 4 1 . 4 2 . 4 3 . 4 4 . 4 5 . 4 6 . 4 7 . 4 8 . 4 9 . 5 0 . 5 1 . 5 2 . 5 3 . 5 4 . 5 5 . 5 6 . 5 7 . 5 8 . 5 9 . 6 0 . 6 1 . 6 2 . 6 3 . 6 4 . 6 5 . 6 6 . 6 7 . 6 8 . 6 9 . 7 0 . 7 1 .
www.ianatkinson.net/computing/adcsharp.htm
4/13
12/12/2013
< s n i p >
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 . 1 5 . 1 6 . 1 7 . 1 8 . 1 9 . 2 0 . 2 1 . 2 2 . 2 3 . 2 4 . 2 5 . 2 6 . 2 7 . 2 8 . 2 9 . 3 0 . 3 1 . 3 2 . 3 3 . 3 4 . 3 5 . 3 6 . 3 7 . 3 8 . 3 9 . 4 0 . 4 1 . 4 2 . 4 3 . 4 4 . 4 5 . 4 6 . 4 7 . 4 8 . 4 9 . 5 0 . 5 1 . 5 2 . 5 3 . 5 4 . 5 5 . 5 6 . 5 7 .
u s i n gS y s t e m ; u s i n gS y s t e m . T e x t ; u s i n gS y s t e m . D i r e c t o r y S e r v i c e s ; n a m e s p a c ea c t i v e D i r e c t o r y L d a p E x a m p l e s { c l a s sP r o g r a m { s t a t i cv o i dM a i n ( s t r i n g [ ]a r g s ) { C o n s o l e . W r i t e ( " E n t e ru s e r :" ) ; S t r i n gu s e r n a m e=C o n s o l e . R e a d L i n e ( ) ; t r y { D i r e c t o r y E n t r ym y L d a p C o n n e c t i o n=c r e a t e D i r e c t o r y E n t r y ( ) ; D i r e c t o r y S e a r c h e rs e a r c h=n e wD i r e c t o r y S e a r c h e r ( m y L d a p C o n n e c t i o n ) ; s e a r c h . F i l t e r=" ( c n = "+u s e r n a m e+" ) " ; / /c r e a t ea na r r a yo fp r o p e r t i e st h a tw ew o u l dl i k ea n d / /a d dt h e mt ot h es e a r c ho b j e c t s t r i n g [ ]r e q u i r e d P r o p e r t i e s=n e ws t r i n g [ ] { " c n " ," p o s t o f f i c e b o x " ," m a i l " } ; f o r e a c h( S t r i n gp r o p e r t yi nr e q u i r e d P r o p e r t i e s ) s e a r c h . P r o p e r t i e s T o L o a d . A d d ( p r o p e r t y ) ; S e a r c h R e s u l tr e s u l t=s e a r c h . F i n d O n e ( ) ; i f( r e s u l t! =n u l l ) { f o r e a c h( S t r i n gp r o p e r t yi nr e q u i r e d P r o p e r t i e s ) f o r e a c h( O b j e c tm y C o l l e c t i o ni nr e s u l t . P r o p e r t i e s [ p r o p e r t y ] ) C o n s o l e . W r i t e L i n e ( S t r i n g . F o r m a t ( " { 0 , 2 0 }:{ 1 } " , p r o p e r t y ,m y C o l l e c t i o n . T o S t r i n g ( ) ) ) ; } e l s eC o n s o l e . W r i t e L i n e ( " U s e rn o tf o u n d ! " ) ; } c a t c h( E x c e p t i o ne ) { C o n s o l e . W r i t e L i n e ( " E x c e p t i o nc a u g h t : \ n \ n "+e . T o S t r i n g ( ) ) ; } } s t a t i cD i r e c t o r y E n t r yc r e a t e D i r e c t o r y E n t r y ( ) { / /c r e a t ea n dr e t u r nn e wL D A Pc o n n e c t i o nw i t hd e s i r e ds e t t i n g s D i r e c t o r y E n t r yl d a p C o n n e c t i o n =n e wD i r e c t o r y E n t r y ( " r i z z o . l e e d s a r t . a c . u k " ) ; l d a p C o n n e c t i o n . P a t h =" L D A P : / / O U = s t a f f u s e r s , D C = l e e d s a r t , D C = a c , D C = u k " ; l d a p C o n n e c t i o n . A u t h e n t i c a t i o n T y p e=A u t h e n t i c a t i o n T y p e s . S e c u r e ; r e t u r nl d a p C o n n e c t i o n ; } } }
www.ianatkinson.net/computing/adcsharp.htm
5/13
12/12/2013
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 . 1 5 . 1 6 . 1 7 . 1 8 . 1 9 . 2 0 . 2 1 . 2 2 . 2 3 . 2 4 . 2 5 . 2 6 . 2 7 . 2 8 . 2 9 . 3 0 . 3 1 . 3 2 . 3 3 . 3 4 . 3 5 . 3 6 . 3 7 . 3 8 . 3 9 . 4 0 . 4 1 . 4 2 . 4 3 . 4 4 . 4 5 . 4 6 . 4 7 . 4 8 . 4 9 . 5 0 . 5 1 .
u s i n gS y s t e m ; u s i n gS y s t e m . T e x t ; u s i n gS y s t e m . D i r e c t o r y S e r v i c e s ; n a m e s p a c ea c t i v e D i r e c t o r y L d a p E x a m p l e s { c l a s sP r o g r a m { s t a t i cv o i dM a i n ( s t r i n g [ ]a r g s ) { C o n s o l e . W r i t e ( " E n t e rp r o p e r t y :" ) ; S t r i n gp r o p e r t y=C o n s o l e . R e a d L i n e ( ) ; t r y { D i r e c t o r y E n t r ym y L d a p C o n n e c t i o n=c r e a t e D i r e c t o r y E n t r y ( ) ; D i r e c t o r y S e a r c h e rs e a r c h=n e wD i r e c t o r y S e a r c h e r ( m y L d a p C o n n e c t i o n ) ; s e a r c h . P r o p e r t i e s T o L o a d . A d d ( " c n " ) ; s e a r c h . P r o p e r t i e s T o L o a d . A d d ( p r o p e r t y ) ; S e a r c h R e s u l t C o l l e c t i o na l l U s e r s=s e a r c h . F i n d A l l ( ) ; f o r e a c h ( S e a r c h R e s u l tr e s u l ti na l l U s e r s ) { i f( r e s u l t . P r o p e r t i e s [ " c n " ] . C o u n t>0& &r e s u l t . P r o p e r t i e s [ p r o p e r t y ] . C o u n t>0 ) { C o n s o l e . W r i t e L i n e ( S t r i n g . F o r m a t ( " { 0 , 2 0 }:{ 1 } " , r e s u l t . P r o p e r t i e s [ " c n " ] [ 0 ] . T o S t r i n g ( ) , r e s u l t . P r o p e r t i e s [ p r o p e r t y ] [ 0 ] . T o S t r i n g ( ) ) ) ; } } } c a t c h( E x c e p t i o ne ) { C o n s o l e . W r i t e L i n e ( " E x c e p t i o nc a u g h t : \ n \ n "+e . T o S t r i n g ( ) ) ; } } s t a t i cD i r e c t o r y E n t r yc r e a t e D i r e c t o r y E n t r y ( ) { / /c r e a t ea n dr e t u r nn e wL D A Pc o n n e c t i o nw i t hd e s i r e ds e t t i n g s D i r e c t o r y E n t r yl d a p C o n n e c t i o n=n e wD i r e c t o r y E n t r y ( " r i z z o . l e e d s a r t . a c . u k " ) ; l d a p C o n n e c t i o n . P a t h=" L D A P : / / O U = s t a f f u s e r s , D C = l e e d s a r t , D C = a c , D C = u k " ; l d a p C o n n e c t i o n . A u t h e n t i c a t i o n T y p e=A u t h e n t i c a t i o n T y p e s . S e c u r e ; r e t u r nl d a p C o n n e c t i o n ; } } }
www.ianatkinson.net/computing/adcsharp.htm
6/13
12/12/2013
update_user.cs:
view plain
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 . 1 5 . 1 6 . 1 7 . 1 8 . 1 9 . 2 0 . 2 1 . 2 2 . 2 3 . 2 4 . 2 5 . 2 6 . 2 7 . 2 8 . 2 9 . 3 0 . 3 1 . 3 2 . 3 3 . 3 4 . 3 5 . 3 6 . 3 7 . 3 8 . 3 9 . 4 0 . 4 1 . 4 2 . 4 3 . 4 4 . 4 5 . 4 6 . 4 7 . 4 8 . 4 9 . 5 0 . 5 1 . 5 2 . 5 3 . 5 4 . 5 5 . 5 6 . 5 7 . 5 8 . 5 9 . 6 0 . 6 1 . 6 2 . 6 3 . 6 4 . 6 5 . 6 6 .
u s i n gS y s t e m ; u s i n gS y s t e m . T e x t ; u s i n gS y s t e m . D i r e c t o r y S e r v i c e s ; n a m e s p a c ea c t i v e D i r e c t o r y L d a p E x a m p l e s { c l a s sP r o g r a m { s t a t i cv o i dM a i n ( s t r i n g [ ]a r g s ) { C o n s o l e . W r i t e ( " E n t e ru s e r :" ) ; S t r i n gu s e r n a m e=C o n s o l e . R e a d L i n e ( ) ; t r y { D i r e c t o r y E n t r ym y L d a p C o n n e c t i o n=c r e a t e D i r e c t o r y E n t r y ( ) ; D i r e c t o r y S e a r c h e rs e a r c h=n e wD i r e c t o r y S e a r c h e r ( m y L d a p C o n n e c t i o n ) ; s e a r c h . F i l t e r=" ( c n = "+u s e r n a m e+" ) " ; s e a r c h . P r o p e r t i e s T o L o a d . A d d ( " t i t l e " ) ; S e a r c h R e s u l tr e s u l t=s e a r c h . F i n d O n e ( ) ; i f( r e s u l t! =n u l l ) { / /c r e a t en e wo b j e c tf r o ms e a r c hr e s u l t D i r e c t o r y E n t r ye n t r y T o U p d a t e=r e s u l t . G e t D i r e c t o r y E n t r y ( ) ; / /s h o we x i s t i n gt i t l e C o n s o l e . W r i t e L i n e ( " C u r r e n tt i t l e :"+ e n t r y T o U p d a t e . P r o p e r t i e s [ " t i t l e " ] [ 0 ] . T o S t r i n g ( ) ) ; C o n s o l e . W r i t e ( " \ n \ n E n t e rn e wt i t l e:" ) ; / /g e tn e wt i t l ea n dw r i t et oA D S t r i n gn e w T i t l e=C o n s o l e . R e a d L i n e ( ) ; e n t r y T o U p d a t e . P r o p e r t i e s [ " t i t l e " ] . V a l u e=n e w T i t l e ; e n t r y T o U p d a t e . C o m m i t C h a n g e s ( ) ; C o n s o l e . W r i t e L i n e ( " \ n \ n . . . n e wt i t l es a v e d " ) ; } e l s eC o n s o l e . W r i t e L i n e ( " U s e rn o tf o u n d ! " ) ; } c a t c h( E x c e p t i o ne ) { C o n s o l e . W r i t e L i n e ( " E x c e p t i o nc a u g h t : \ n \ n "+e . T o S t r i n g ( ) ) ; } } s t a t i cD i r e c t o r y E n t r yc r e a t e D i r e c t o r y E n t r y ( ) { / /c r e a t ea n dr e t u r nn e wL D A Pc o n n e c t i o nw i t hd e s i r e ds e t t i n g s D i r e c t o r y E n t r yl d a p C o n n e c t i o n =n e wD i r e c t o r y E n t r y ( " r i z z o . l e e d s a r t . a c . u k " ) ; l d a p C o n n e c t i o n . P a t h =" L D A P : / / O U = s t a f f u s e r s , D C = l e e d s a r t , D C = a c , D C = u k " ; l d a p C o n n e c t i o n . A u t h e n t i c a t i o n T y p e=A u t h e n t i c a t i o n T y p e s . S e c u r e ; r e t u r nl d a p C o n n e c t i o n ; } } }
Here is an (abbreviated) example of the output. Note how when the program is run for the second time the title that is retrieved is the one entered the first time around:
H : \ D e s k t o p \ a d c s h a r p > u p d a t e _ u s e r E n t e ru s e r C u r r e n tt i t l e :I a nA t k i n s o n :S e n i o rI n f r a s t r u c t u r eS u p p o r tE n g i n e e r
E n t e rn e wt i t l e:D o g s b o d y . . . n e wt i t l es a v e d
www.ianatkinson.net/computing/adcsharp.htm
7/13
12/12/2013
E n t e rn e wt i t l e:S e n i o rI n f r a s t r u c t u r eS u p p o r tE n g i n e e r . . . n e wt i t l es a v e d
Obviously if you wanted to use this as a basis for your own program you would need to set the options to your own requirements and tweak as necessary. Specifically if you want to write a flexible program to write users in and out of different OUs, rather than a single OU, then it will be necessary to create multiple LDAP connections with different paths, and also a more complex function to add users to groups which searches the whole subtree. create_user.cs:
view plain
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 . 1 5 . 1 6 . 1 7 . 1 8 . 1 9 . 2 0 . 2 1 . 2 2 . 2 3 . 2 4 . 2 5 . 2 6 . 2 7 . 2 8 . 2 9 . 3 0 . 3 1 . 3 2 . 3 3 . 3 4 . 3 5 . 3 6 . 3 7 . 3 8 . 3 9 . 4 0 . 4 1 .
u s i n gS y s t e m ; u s i n gS y s t e m . T e x t ; u s i n gS y s t e m . D i r e c t o r y S e r v i c e s ; u s i n gS y s t e m . I O ; u s i n gS y s t e m . S e c u r i t y . A c c e s s C o n t r o l ; u s i n gS y s t e m . S e c u r i t y . P r i n c i p a l ; n a m e s p a c ea c t i v e D i r e c t o r y L d a p E x a m p l e s { c l a s sP r o g r a m { s t a t i cv o i dM a i n ( s t r i n g [ ]a r g s ) { / /c o n n e c tt oL D A P D i r e c t o r y E n t r ym y L d a p C o n n e c t i o n=c r e a t e D i r e c t o r y E n t r y ( ) ; / /d e f i n ev a r sf o ru s e r S t r i n gd o m a i n =" l e e d s a r t . a c . u k " ; S t r i n gf i r s t =" T e s t " ; S t r i n gl a s t =" U s e r " ; S t r i n gd e s c r i p t i o n=" . N E TT e s t " ; o b j e c t [ ]p a s s w o r d ={" 1 2 3 4 5 6 7 8 "} ; S t r i n g [ ]g r o u p s ={" S t a f f "} ; S t r i n gu s e r n a m e =f i r s t . T o L o w e r ( )+l a s t . S u b s t r i n g ( 0 ,1 ) . T o L o w e r ( ) ; S t r i n gh o m e D r i v e =" H : " ; S t r i n gh o m e D i r =@ " \ \ g o n z o . l e e d s a r t . a c . u k \ d a t a 3 \ U S E R S \ "+u s e r n a m e ; / /c r e a t eu s e r t r y { i f( c r e a t e U s e r ( m y L d a p C o n n e c t i o n ,d o m a i n ,f i r s t ,l a s t ,d e s c r i p t i o n , p a s s w o r d ,g r o u p s ,u s e r n a m e ,h o m e D r i v e ,h o m e D i r ,t r u e )= =0 ) { C o n s o l e . W r i t e L i n e ( " A c c o u n tc r e a t e d ! " ) ; C o n s o l e . R e a d L i n e ( ) ; }
www.ianatkinson.net/computing/adcsharp.htm
8/13
12/12/2013
4 2 . 4 3 . 4 4 . 4 5 . 4 6 . 4 7 . 4 8 . 4 9 . 5 0 . 5 1 . 5 2 . 5 3 . 5 4 . 5 5 . 5 6 . 5 7 . 5 8 . 5 9 . 6 0 . 6 1 . 6 2 . 6 3 . 6 4 . 6 5 . 6 6 . 6 7 . 6 8 . 6 9 . 7 0 . 7 1 . 7 2 . 7 3 . 7 4 . 7 5 . 7 6 . 7 7 . 7 8 . 7 9 . 8 0 . 8 1 . 8 2 . 8 3 . 8 4 . 8 5 . 8 6 . 8 7 . 8 8 . 8 9 . 9 0 . 9 1 . 9 2 . 9 3 . 9 4 . 9 5 . 9 6 . 9 7 . 9 8 . 9 9 . 1 0 0 . 1 0 1 . 1 0 2 . 1 0 3 . 1 0 4 . 1 0 5 . 1 0 6 . 1 0 7 . 1 0 8 . 1 0 9 . 1 1 0 . 1 1 1 . 1 1 2 . 1 1 3 . 1 1 4 . 1 1 5 . 1 1 6 . 1 1 7 . 1 1 8 . 1 1 9 . 1 2 0 . 1 2 1 . 1 2 2 . 1 2 3 . 1 2 4 . 1 2 5 .
www.ianatkinson.net/computing/adcsharp.htm
9/13
12/12/2013
1 2 6 . 1 2 7 . 1 2 8 . 1 2 9 . 1 3 0 . 1 3 1 . 1 3 2 . 1 3 3 . 1 3 4 . 1 3 5 . 1 3 6 . 1 3 7 . 1 3 8 . 1 3 9 . 1 4 0 . 1 4 1 . 1 4 2 . 1 4 3 . 1 4 4 . 1 4 5 . 1 4 6 . 1 4 7 . 1 4 8 . 1 4 9 . 1 5 0 . 1 5 1 . 1 5 2 . 1 5 3 . 1 5 4 . 1 5 5 . 1 5 6 . 1 5 7 . 1 5 8 . 1 5 9 . 1 6 0 . 1 6 1 . 1 6 2 . 1 6 3 . 1 6 4 . 1 6 5 . 1 6 6 . 1 6 7 . 1 6 8 . 1 6 9 . 1 7 0 . 1 7 1 . 1 7 2 . 1 7 3 . 1 7 4 . 1 7 5 . 1 7 6 . 1 7 7 . 1 7 8 . 1 7 9 . 1 8 0 . 1 8 1 . 1 8 2 . 1 8 3 . 1 8 4 .
System.DirectoryServices.AccountManagement Examples
This second set of examples all use the newer libraries and will serve you best if you are writing smaller or simpler programs.
www.ianatkinson.net/computing/adcsharp.htm
10/13
12/12/2013
newer_details.cs:
view plain
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 . 1 5 . 1 6 . 1 7 . 1 8 . 1 9 . 2 0 . 2 1 . 2 2 . 2 3 . 2 4 . 2 5 . 2 6 . 2 7 . 2 8 . 2 9 . 3 0 . 3 1 . 3 2 . 3 3 . 3 4 . 3 5 . 3 6 .
u s i n gS y s t e m ; u s i n gS y s t e m . D i r e c t o r y S e r v i c e s . A c c o u n t M a n a g e m e n t ; n a m e s p a c en e w _ a d _ e x m a p l e { c l a s sP r o g r a m { s t a t i cv o i dM a i n ( s t r i n g [ ]a r g s ) { t r y { / /e n t e rA Ds e t t i n g s P r i n c i p a l C o n t e x tA D=n e wP r i n c i p a l C o n t e x t ( C o n t e x t T y p e . D o m a i n ," l e e d s a r t . a c . u k " ) ; / /c r e a t es e a r c hu s e ra n da d dc r i t e r i a C o n s o l e . W r i t e ( " E n t e rl o g o nn a m e :" ) ; U s e r P r i n c i p a lu=n e wU s e r P r i n c i p a l ( A D ) ; u . S a m A c c o u n t N a m e=C o n s o l e . R e a d L i n e ( ) ; / /s e a r c hf o ru s e r P r i n c i p a l S e a r c h e rs e a r c h=n e wP r i n c i p a l S e a r c h e r ( u ) ; U s e r P r i n c i p a lr e s u l t=( U s e r P r i n c i p a l ) s e a r c h . F i n d O n e ( ) ; s e a r c h . D i s p o s e ( ) ; / /s h o ws o m ed e t a i l s C o n s o l e . W r i t e L i n e ( " D i s p l a yN a m e:"+r e s u l t . D i s p l a y N a m e ) ; C o n s o l e . W r i t e L i n e ( " P h o n eN u m b e r:"+r e s u l t . V o i c e T e l e p h o n e N u m b e r ) ; } c a t c h( E x c e p t i o ne ) { C o n s o l e . W r i t e L i n e ( " E r r o r :"+e . M e s s a g e ) ; } } } }
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 . 1 5 . 1 6 . 1 7 . 1 8 . 1 9 . 2 0 . 2 1 . 2 2 .
www.ianatkinson.net/computing/adcsharp.htm
11/13
12/12/2013
2 3 . 2 4 . 2 5 . 2 6 . 2 7 . 2 8 . 2 9 .
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 . 1 5 . 1 6 . 1 7 . 1 8 . 1 9 . 2 0 . 2 1 . 2 2 . 2 3 . 2 4 . 2 5 . 2 6 . 2 7 . 2 8 . 2 9 . 3 0 . 3 1 . 3 2 . 3 3 . 3 4 . 3 5 . 3 6 . 3 7 . 3 8 . 3 9 .
u s i n gS y s t e m ; u s i n gS y s t e m . D i r e c t o r y S e r v i c e s ; u s i n gS y s t e m . D i r e c t o r y S e r v i c e s . A c c o u n t M a n a g e m e n t ; n a m e s p a c en e w _ a d _ e x m a p l e { c l a s sP r o g r a m { s t a t i cv o i dM a i n ( s t r i n g [ ]a r g s ) { t r y { P r i n c i p a l C o n t e x t A D =n e wP r i n c i p a l C o n t e x t ( C o n t e x t T y p e . D o m a i n ," l e e d s a r t . a c . u k " ) ; U s e r P r i n c i p a l u =n e wU s e r P r i n c i p a l ( A D ) ; P r i n c i p a l S e a r c h e rs e a r c h=n e wP r i n c i p a l S e a r c h e r ( u ) ; f o r e a c h( U s e r P r i n c i p a lr e s u l ti ns e a r c h . F i n d A l l ( ) ) { i f( r e s u l t . V o i c e T e l e p h o n e N u m b e r! =n u l l ) { D i r e c t o r y E n t r yl o w e r L d a p=( D i r e c t o r y E n t r y ) r e s u l t . G e t U n d e r l y i n g O b j e c t ( ) ; C o n s o l e . W r i t e L i n e ( " { 0 , 3 0 }{ 1 }{ 2 } " , r e s u l t . D i s p l a y N a m e , r e s u l t . V o i c e T e l e p h o n e N u m b e r , l o w e r L d a p . P r o p e r t i e s [ " p o s t o f f i c e b o x " ] [ 0 ] . T o S t r i n g ( ) ) ; } } s e a r c h . D i s p o s e ( ) ; } c a t c h( E x c e p t i o ne ) { C o n s o l e . W r i t e L i n e ( " E r r o r :"+e . M e s s a g e ) ; } } } }
www.ianatkinson.net/computing/adcsharp.htm
12/13
12/12/2013
www.ianatkinson.net/computing/adcsharp.htm
13/13