Advertisement
HenX

LDAP - User Info

Sep 14th, 2015
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.83 KB | None | 0 0
  1.    public UserInfo(string login)
  2.         {
  3.             LdapConnection con = new LdapConnection(new LdapDirectoryIdentifier("ldap.vsb.cz", 636));
  4.             con.SessionOptions.SecureSocketLayer = true;
  5.             con.AuthType = AuthType.Anonymous;
  6.             using (con)
  7.             {
  8.                 try
  9.                 {
  10.                     SearchRequest request = new SearchRequest(
  11.                     "",
  12.                     String.Format("(&(objectClass=Person)(uid={0}))", login),
  13.                     System.DirectoryServices.Protocols.SearchScope.Subtree
  14.                     );
  15.                     SearchResponse response = (SearchResponse)con.SendRequest(request);
  16.  
  17.                     if (response.Entries.Count == 0)
  18.                     {
  19.                         // NO
  20.                     }
  21.                     else
  22.                     {
  23.                         SearchResultEntry entry = response.Entries[0];
  24.                         string dn = entry.DistinguishedName;
  25.                         string path = "LDAP://ldap.vsb.cz:389/" + dn;
  26.                         DirectoryEntry dentry = new DirectoryEntry(path);
  27.                         dentry.AuthenticationType = AuthenticationTypes.Anonymous;
  28.  
  29.                         DirectorySearcher search = new DirectorySearcher(dentry);
  30.  
  31.                         SearchResult result = search.FindOne();
  32.                         this.Login = result.Properties["cn"][0].ToString();
  33.                         this.FName = result.Properties["givenname"][0].ToString();
  34.                         this.LName = result.Properties["sn"][0].ToString();
  35.                         this.Email = result.Properties["mail"][0].ToString();
  36.                     }
  37.                 }
  38.                 catch
  39.                 {
  40.                     this.Login = null;
  41.                 }
  42.             }
  43.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement