code_junkie

Simple login for multi-domain intranet

Nov 14th, 2011
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. // NOTE: implement IDisposable and dispose of this if not null when done.
  2. private DirectoryEntry userSearchRoot = null;
  3. private UserPrincipal FindUserInGlobalContext( string userName )
  4. {
  5. using (PrincipalSearcher userSearcher = new PrincipalSearcher())
  6. {
  7. using (PrincipalContext context
  8. = new PrincipalContext( ContextType.Domain ))
  9. {
  10. userSearcher.QueryFilter = new UserPrincipal( context );
  11. DirectorySearcher searcher
  12. = (DirectorySearcher)userSearcher.GetUnderlyingSearcher();
  13.  
  14. // I usually set the GC path from the existing search root
  15. // by doing some string manipulation based on our domain
  16. // Your code would be different.
  17. string GCPath = ...set GC path..
  18.  
  19. // lazy loading of the search root entry.
  20. if (userSearchRoot == null)
  21. {
  22. userSearchRoot = new DirectoryEntry( GCPath );
  23. }
  24.  
  25. searcher.SearchRoot = userSearchRoot;
  26. using (PrincipalContext gcContext =
  27. new PrincipalContext( ContextType.Domain,
  28. null,
  29. GCPath.Replace("GC://",""))
  30. {
  31. UserPrincipal userFilter = new UserPrincipal( gcContext );
  32. userFilter.SamAccountName = userName;
  33. userSearcher.QueryFilter = userFilter;
  34. return userSearcher.FindOne() as UserPrincipal;
  35. }
  36. }
  37. }
  38. }
Add Comment
Please, Sign In to add comment