ログ用にユーザー名を保存する次のメソッドを作成しました:-
if (ModelState.IsValid)
{
string ADusername = User.Identity.Name.Substring(User.Identity.Name.IndexOf("\\") + 1);
repository.InsertOrUpdateTechnologyAssociation(ua, ADusername);
repository.Save();
//code hoes here
}
上記は、管理者のユーザー名をすべて小文字の「administrator」として保存します。次のようにアクティブディレクトリからユーザー名を取得した場合:-
public List<DomainContext> GetADUsers(string term=null)
{
List<DomainContext> results = new List<DomainContext>();
using (var context = new PrincipalContext(ContextType.Domain, "WIN-SPDEV"))
using (var searcher = new PrincipalSearcher(new UserPrincipal(context)))
{
var searchResults = searcher.FindAll();
foreach (Principal p in searchResults)
{
if (term == null || p.SamAccountName.ToString().ToUpper().StartsWith(term.ToUpper()))
{
DomainContext dc = new DomainContext();
dc.DisplayName = p.DisplayName;
dc.UserPrincipalName = p.UserPrincipalName;
dc.Name = p.Name;
dc.SamAccountName = p.SamAccountName ;
dc.DistinguishedName = p.DistinguishedName;
results.Add(dc);
}
}
}
return results;
}
次に、名前と SamAccountName は、大文字の A を最初に使用した「管理者」になります。そのため、ログイン ユーザーと Active Directory のユーザー名を比較しようとすると、管理者 != 管理者であるため、結果は常に false であったため、アプリケーション内で問題が発生しました。したがって、この問題を解決するために、両方の値を小文字に変更して比較することにより、次のように比較を記述しました。
bool isadminByuser = tms.SecurityRoles.Where(a => a.Name == "Administrator").SingleOrDefault().SecurityRoleUsers.Any(a => a.UserName.ToLower() == user.ToLower());
そのような比較は将来問題を引き起こしますか、それとも suername を小文字に変更しても安全ですか?? または、管理者と管理者が 2 人の異なるユーザーを表すことができますか?? ありがとう