ネットワーク内のさまざまなコンピューターで実行されているプログラムがあり、このプログラムは特定の管理者アカウントでログインしていくつかのタスクを実行する必要があります。
サプライヤーは、ユーザーがローカル管理者であるかどうかを確認するために以下のコードを作成しました。
質問は:
- 管理者ユーザーが以前にコンピューターにログインしたことがある場合、資格情報はキャッシュに保存されるので、ネットワークに接続していなくても以下のコードは機能しますか?
- もしそうなら、どうすればそれらの資格情報を強制的に保存できますか?
そうでなければ、キャッシュからの資格情報でログインできるようにするための代替手段はありますか?
static bool UserExists(string domain, string username, string password) { if (System.Environment.GetEnvironmentVariable("UserDomain") != null) { if (string.Compare(System.Environment.GetEnvironmentVariable("UserDomain"), domain, true) != 0) { return false; } } string filter = string.Format("(&(ObjectClass={0})(sAMAccountName={1}))", "person", username); string[] properties = new string[] { "fullname" }; using (DirectoryEntry adRoot = new DirectoryEntry("LDAP://" + domain, null, null, AuthenticationTypes.Secure)) using (DirectorySearcher searcher = new DirectorySearcher(adRoot)) { adRoot.Username = username; adRoot.Password = password; searcher.SearchScope = SearchScope.Subtree; searcher.ReferralChasing = ReferralChasingOption.All; searcher.PropertiesToLoad.AddRange(properties); searcher.Filter = filter; if (searcher.FindOne() != null) { // If you want to see the user details: searcher.FindOne().GetDirectoryEntry(); return true; } } return false; }
前もって感謝します