MVC アプリケーションのある時点で、Active Directory を検索して、そのパターンを含むユーザー アカウントを取得します。問題は、私の会社では、一部のユーザーの UserPrincipalName にアクセント記号/分音記号があり、アクセント記号を使用して検索すると、それらのユーザーが存在しないことです。しかし、アクセントなしで検索すると、アプリケーションはそれらのユーザーを見つけます。
文字列を Unicode に変換しようとしましたが、うまくいきません。this、 this、this、および見つけられない他のいくつかを使用しました。
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;
}