In some point of an MVC application, I do a search to Active Directory to get users account that contains that pattern. The problem is, at my company, some users have accents/diacritics on their UserPrincipalName and when I do the search, with the accents, those users don't exist. But if I do the search without the accents, the application find those users.
I already tried to convert the string to Unicode, but doesn't work. I used this, this, this and some others that I can't find.
public static List<string> SearchUsername(string __Pattern)
{
__Pattern = __Pattern.Normalize(NormalizationForm.FormD);
var chars = __Pattern.Where(c => CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark).ToArray();
__Pattern = new string(chars).Normalize(NormalizationForm.FormC);
List<string> Result = new List<string>();
PrincipalContext Ldap = new PrincipalContext(ContextType.Domain, ConfigurationManager.AppSettings["LdapConnection"]);
UserPrincipal User = new UserPrincipal(Ldap);
User.UserPrincipalName = __Pattern + "*@cebi.org.pt";
PrincipalSearcher Search = new PrincipalSearcher(User);
foreach (var UserFound in Search.FindAll())
{
Result.Add(UserFound.UserPrincipalName.ToString().Split('@').First());
}
return Result;
}