Created
December 28, 2016 07:03
-
-
Save arman-hpp/98f2220082938f0fe4bb9531959b0e6f to your computer and use it in GitHub Desktop.
Active Directory Helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class ActiveDirectoryHelper | |
{ | |
public static string GetSystemDomain() | |
{ | |
try | |
{ | |
return Domain.GetComputerDomain().ToString().ToLower(); | |
} | |
catch (Exception ex) | |
{ | |
//LOG | |
return string.Empty; | |
} | |
} | |
public static ActiveDirectoryUserInfo GetUserInformation(string username, string passowrd, string loginUserName) | |
{ | |
Cursor.Current = Cursors.WaitCursor; | |
var domain = GetSystemDomain(); | |
if (string.IsNullOrEmpty(domain)) return null; | |
var rs = SearchUserByUserName(GetDirectorySearcher(username, passowrd, domain), loginUserName); | |
if (rs == null) return null; | |
var activeDirectoryUser = new ActiveDirectoryUserInfo(); | |
if (rs.GetDirectoryEntry().Properties["samaccountname"].Value != null) | |
activeDirectoryUser.UsernameDisplay = | |
rs.GetDirectoryEntry().Properties["samaccountname"].Value.ToString(); | |
if (rs.GetDirectoryEntry().Properties["givenName"].Value != null) | |
activeDirectoryUser.Firstname = rs.GetDirectoryEntry().Properties["givenName"].Value.ToString(); | |
if (rs.GetDirectoryEntry().Properties["initials"].Value != null) | |
activeDirectoryUser.MiddleName = rs.GetDirectoryEntry().Properties["initials"].Value.ToString(); | |
if (rs.GetDirectoryEntry().Properties["sn"].Value != null) | |
activeDirectoryUser.LastName = rs.GetDirectoryEntry().Properties["sn"].Value.ToString(); | |
if (rs.GetDirectoryEntry().Properties["mail"].Value != null) | |
activeDirectoryUser.EmailId = rs.GetDirectoryEntry().Properties["mail"].Value.ToString(); | |
if (rs.GetDirectoryEntry().Properties["title"].Value != null) | |
activeDirectoryUser.Title = rs.GetDirectoryEntry().Properties["title"].Value.ToString(); | |
if (rs.GetDirectoryEntry().Properties["company"].Value != null) | |
activeDirectoryUser.Company = rs.GetDirectoryEntry().Properties["company"].Value.ToString(); | |
if (rs.GetDirectoryEntry().Properties["l"].Value != null) | |
activeDirectoryUser.City = rs.GetDirectoryEntry().Properties["l"].Value.ToString(); | |
if (rs.GetDirectoryEntry().Properties["st"].Value != null) | |
activeDirectoryUser.State = rs.GetDirectoryEntry().Properties["st"].Value.ToString(); | |
if (rs.GetDirectoryEntry().Properties["co"].Value != null) | |
activeDirectoryUser.Country = rs.GetDirectoryEntry().Properties["co"].Value.ToString(); | |
if (rs.GetDirectoryEntry().Properties["postalCode"].Value != null) | |
activeDirectoryUser.Postal = rs.GetDirectoryEntry().Properties["postalCode"].Value.ToString(); | |
if (rs.GetDirectoryEntry().Properties["telephoneNumber"].Value != null) | |
activeDirectoryUser.Telephone = | |
rs.GetDirectoryEntry().Properties["telephoneNumber"].Value.ToString(); | |
return activeDirectoryUser; | |
} | |
private static DirectorySearcher GetDirectorySearcher(string username, string passowrd, string domain) | |
{ | |
try | |
{ | |
return new DirectorySearcher( | |
new DirectoryEntry("LDAP://" + domain, username, passowrd)); | |
} | |
catch (DirectoryServicesCOMException ex) | |
{ | |
//Log | |
return null; | |
} | |
} | |
private static SearchResult SearchUserByUserName(DirectorySearcher ds, string username) | |
{ | |
ds.Filter = "(&((&(objectCategory=Person)(objectClass=User)))(samaccountname=" + username + "))"; | |
ds.SearchScope = SearchScope.Subtree; | |
ds.ServerTimeLimit = TimeSpan.FromSeconds(90); | |
return ds.FindOne(); | |
} | |
private static SearchResult SearchUserByEmail(DirectorySearcher ds, string email) | |
{ | |
ds.Filter = "(&((&(objectCategory=Person)(objectClass=User)))(mail=" + email + "))"; | |
ds.SearchScope = SearchScope.Subtree; | |
ds.ServerTimeLimit = TimeSpan.FromSeconds(90); | |
return ds.FindOne(); | |
} | |
} | |
public class ActiveDirectoryUserInfo | |
{ | |
public string UsernameDisplay { get; set; } | |
public string Firstname { get; set; } | |
public string MiddleName { get; set; } | |
public string LastName { get; set; } | |
public string EmailId { get; set; } | |
public string Title { get; set; } | |
public string Company { get; set; } | |
public string City { get; set; } | |
public string State { get; set; } | |
public string Country { get; set; } | |
public string Postal { get; set; } | |
public string Telephone { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment