Reputation: 27
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;
}
Upvotes: 1
Views: 747
Reputation: 10287
The link contains few solutions, some of the ignore several characters used in different languages.
The following code is in one of the answers of one of them and seems to work better even though it is not signed as the correct answer.
string accentedStr;
byte[] tempBytes;
tempBytes = System.Text.Encoding.GetEncoding("ISO-8859-8").GetBytes(accentedStr);
string asciiStr = System.Text.Encoding.UTF8.GetString(tempBytes);
This is done by converting the text for Hebrew Unicode
which lacks the characters with accents, and encode it back to UTF8
.
Try if this method fits your character set.
Upvotes: 1