Created
November 13, 2017 18:15
-
-
Save Memphizzz/6b490ca8e2477704a30f56ca0d489575 to your computer and use it in GitHub Desktop.
ActiveDirectoryHelper and Info
This file contains 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 class ActiveDirectoryHelper : IDisposable | |
{ | |
private readonly DirectoryEntry _de; | |
private readonly DirectorySearcher _searcher; | |
public ActiveDirectoryHelper(string ldapPath) | |
{ | |
_de = new DirectoryEntry(ldapPath); | |
_searcher = new DirectorySearcher(_de); | |
} | |
public ActiveDirectoryInfo GetActiveDirectoryInfo(Storage.Recipient recipient) | |
{ | |
return GetActiveDirectoryInfoInternal(recipient.Email, ResolvePartyType(recipient.Type.Value)); | |
} | |
public ActiveDirectoryInfo GetActiveDirectoryInfo(Storage.SenderRepresenting sender) | |
{ | |
return GetActiveDirectoryInfoInternal(sender.Email, PartyTypes.From); | |
} | |
private ActiveDirectoryInfo GetActiveDirectoryInfoInternal(string adString, PartyTypes partyType) | |
{ | |
if (string.IsNullOrWhiteSpace(adString)) | |
return null; | |
var searchTerm = "CN="; | |
var index = adString.LastIndexOf(searchTerm, StringComparison.Ordinal); | |
var username = adString.Substring(index + searchTerm.Length); | |
var filter = "(|(sAMAccountName=" + username + ")(mailNickname=" + username + "))"; | |
_searcher.Filter = filter; | |
_searcher.PropertiesToLoad.Add("mail"); | |
_searcher.PropertiesToLoad.Add("mailNickname"); | |
_searcher.PropertiesToLoad.Add("sAMAccountName"); | |
_searcher.PropertiesToLoad.Add("displayName"); | |
var result = _searcher.FindOne(); | |
if (result == null) | |
{ | |
filter = "(legacyExchangeDN=" + adString + ")"; | |
_searcher.Filter = filter; | |
result = _searcher.FindOne(); | |
} | |
if (result == null) | |
throw new InvalidDataException("Cannot resolve email address from '" + filter + "'!"); | |
string accountName = null; | |
if (result.Properties.Contains("sAMAccountName")) | |
accountName = result.Properties["sAMAccountName"][0].ToString(); | |
return new ActiveDirectoryInfo( | |
result.Properties["displayName"][0].ToString(), | |
accountName, | |
result.Properties["mailNickname"][0].ToString(), | |
new MailAddress(result.Properties["mail"][0].ToString()), | |
partyType | |
); | |
} | |
public static PartyTypes ResolvePartyType(Storage.Recipient.RecipientType type) | |
{ | |
if (!Enum.TryParse(type.ToString(), out PartyTypes partyType)) | |
throw new InvalidDataException("Unable to resolve PartyType!"); | |
return partyType; | |
} | |
public void Dispose() | |
{ | |
_de?.Dispose(); | |
_searcher?.Dispose(); | |
} | |
} | |
public class ActiveDirectoryInfo | |
{ | |
public string DisplayName { get; private set; } | |
public string UserName { get; private set; } | |
public string MailNickname { get; private set; } | |
public MailAddress MailAddress { get; private set; } | |
public PartyTypes PartyType { get; set; } | |
public ActiveDirectoryInfo(string displayName, string userName, string mailNickname, MailAddress mailAddress, PartyTypes partyType) | |
{ | |
DisplayName = displayName; | |
UserName = userName; | |
MailNickname = mailNickname; | |
MailAddress = mailAddress; | |
PartyType = partyType; | |
} | |
public ActiveDirectoryInfo(Storage.Recipient recipient) | |
{ | |
string email = recipient.Email; | |
if (recipient.DisplayName == email || !email.IsValidEmail()) | |
{ | |
if (!email.IsValidEmail()) | |
{ | |
var regex = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", RegexOptions.IgnoreCase); | |
var match = regex.Match(email); | |
if (match.Success) | |
email = match.Value; | |
else | |
//throw new InvalidDataException("Unable to parse email address!"); | |
email = "[email protected]"; | |
} | |
} | |
MailAddress = new MailAddress(email); | |
DisplayName = recipient.DisplayName; | |
PartyType = ActiveDirectoryHelper.ResolvePartyType(recipient.Type.Value); | |
} | |
} | |
public enum PartyTypes | |
{ | |
From = 0, | |
To = 1, | |
Cc = 2, | |
Bcc = 3, | |
Room = 4, | |
Resource = 5, | |
Unknown = 999 | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment