ホームページ  >  記事  >  バックエンド開発  >  C# を使用した Active Directory

C# を使用した Active Directory

PHPz
PHPzオリジナル
2024-09-03 15:33:36482ブラウズ

C# を使用した Active Directory は、何よりもまずコンピューターや企業ユーザーなどを整理します。重要なのは、企業ネットワークやビジネス目的で一般的に使用されるユーザー管理システムです。会社管理者は、ネットワークに属するコンピュータ、プロファイル、アクセス許可などから会社の全履歴を整理するために使用します。 Active Directory は、Oracle や SQL のようなデータベースの一種であり、独自のクエリ タイプ言語と LDAP に基づく一連のルールを持っています。

構文:

DirectoryEntry オブジェクトにアクセスするには、Active Directory のユーザー名、パスワード、サーバー アドレスが必要です。サーバーアドレスには IP アドレスまたは LDAP のパスが含まれており、構文は次のようになります。

LDAP://domain_name.com/DC=domain_name,DC=com

C# で Active Directory はどのように機能しますか?

Active Directory は IT 管理者にとって非常に役立ち、会社のユーザー、デスクトップなどを整理するのに役立ちます。全体の階層構造により、どのネットワークに属しているか、プロフィール写真に何が表示されているか、誰がストレージにアクセスしているかなどを把握できます。ほとんどの企業は、アプリケーションをデスクトップから、フォームベースの LDAP 認証を保持する Web アプリケーションに移行させています。場合によっては、.NET アプリケーションは Microsoft Active Directory (AD) と対話して、ユーザーのリストを取得し、グループを検索し、ユーザーを認証し、さらにどのユーザーがどの Active Directory グループに属しているかを確認します。いくつかのアプローチを使用すると、ドメイン内の AD データベースから情報を取得できます。

さまざまなアプローチがあります。その 1 つは、ネームスペース System.DirectoryServices に属するクラス DirectoryEntry および DirectorySearch を含む LDAP (Lightweight Directory Access Protocol) を利用することです。もう 1 つのアプローチは、Active Directory (AD) の名前空間 System.DirectoryServices.AccountManagement のクラス ラッパーのセット全体を利用することです。 LDAP クエリを使用すると、AD データベースから情報を取得できます。このクラスを使用すると AD 全体にアクセスできますが、ラッパー クラスを使用すると AD 内のユーザー、コンピューター オブジェクト、グループを取得できます。 DirectoryEntry クラスと DirectorySearch オブジェクトは、System.DirectoryServices.AccountManagement.

オブジェクトより高速です。

C# を使用した Active Directory の基本には System.DirectoryService ライブラリが含まれており、AD ライブラリ ルーチンを使用したクエリに役立ちます。 Active Directory は、DirectoryEntry のオブジェクトを通じて通信します。これらのオブジェクトは、追加のオブジェクトやフォルダーをクエリできる LDAP データベースへの最も重要な接続です。 DirectoryEntry オブジェクトにアクセスするには、Active Directory のユーザー名、パスワード、およびサーバー アドレスが必要です。サーバーアドレスには IP アドレスまたは LDAP のパスが含まれており、次のようになります。

LDAP://domain_name.com/DC=domain_name,DC=com

C# を使用した Active Directory の例

DirectoryEntry のオブジェクトに対して Active Directory に接続するには、Active Directory への安全な認証接続を示す安全な認証タイプのユーザーを作成する必要があります。管理者のアカウントに接続すると、新しいユーザーの作成、ユーザーの更新、ユーザーの削除などの管理 Active Directory 機能を実行できます。

ディレクトリ オブジェクトの DirectoryEntry を取得します

private DirectoryEntry Reterieve_DirectoryObject( )
{
DirectoryEntry Obj_de;
Obj_de=new DirectoryEntry("LDAP://IP_Address", "admin","password", AuthenticationTypes Secure);
return _de;
}

C# で Active Directory からユーザーを取得します

Active Directory に接続したら、以下のようにユーザーを取得するなど、オブジェクトをクエリする必要があります。

private DirectoryEntry Reterieve_User(string User_Name)
{
DirectoryEntry obj_de = Reterieve_DirectoryObject( );
DirectorySearcher obj_deSearch = new DirectorySearcher();
obj_deSearch.SearchRoot = obj_de;
obj_deSearch.Filter = "(&(objectClass=user)(JOHNAccountName=" + User_Name + "))";
obj_deSearch.SearchScope = SearchScope.Subtree;
SearchResult getPath = obj_deSearch.FindOne();
if (!(getPath == null))
{
obj_de = new DirectoryEntry(getPath.Path, "administrator", "password", AuthenticationTypes.Secure);
return obj_de;
}
else
{
return null;
}
}

上記のコードは、ログイン認証情報に応じて Active Directory からユーザーを取得する方法を説明しています。括弧で囲まれた特定の Active Directory クエリ言語を使用する必要があります。この言語には、Active Directory と同様に「JohnAccountName」などの名前とユーザー名が含まれます。 DirectoryEntry を見つけたら、次に結果にリンクする新しい DirectoryEntry オブジェクトをコーディングし、接続で管理者のログイン詳細を使用します。

認証ユーザーの作成

Active Directory の認証済みユーザーを作成するには、有効な LDAP パス文字列を DirectoryEntry クラス コンストラクターに渡す必要があります。これは LDAP://Doamin_name の形式に従います。次の方法を見てみましょう。

private bool AuthenticateUser(string domain_name, string user_name, string password)
{
bool result = false;
try
{
DirectoryEntry obj_de = new DirectoryEntry("LDAP://" + domainName, userName, password);
DirectorySearcher obj_dsearch = new DirectorySearcher(obj_de);
SearchResult _sResult = null;
sResult = obj_dsearch.FindOne();
result = true;
}
catch
{
result = false;
}
return result;
}

C# で Active Directory のユーザーの詳細を変更する

C# で Active Directory のオブジェクトのプロパティを変更することは、C# では非常に簡単です。まず、DirectoryEntry プロパティ フィールドにアクセスし、必要に応じて値を変更し、その前に値が null かどうかを確認してから、最後に関数を呼び出します。 ComminChanges を実行して、行われた変更を保存します。ユーザーの名前プロパティを変更する以下のコードを見てみましょう

DirectoryEntry obj_de = Reterieve_User ("smith.rio");
if (obj_de!= null)
{
if (obj_de.Properties["displayName"] != null && obj_de.Properties["displayName"].Value != null)
{
de.Properties["displayName"].Value = "Smith, Rio (Welcome)";
de.CommitChanges();
}
}

The above code describes the CommitChanges(), which will save the changes made in the Active Directory. The most important thing is that whatever changes are made will not be immediately visible in the applications of Active Directory like users of Active Directory and computers in the control panel it takes around 5-30 minutes to visible during changes because it needs to synchronize over the servers all through the network.

Querying Multiple Users in Active Directory with C# ASP .NET

The code above explained was to query the single DirectoryEntry object, whereas if we required for the list of objects we need to use the type SearchResultCollection joined with obj_deSearch, to search out all the things instead of finding one,

SearchResultCollection findUsers = retrieve_allUsers();
if (findUsers!= null && findUsers.Count > 0)
{
foreach (SearchResult getUser in findUsers)
{
DirectoryEntry obj_de = getUser.GetDirectoryEntry();
}
}

Conclusion

In this article, I have explained about the Active Directory and how to retrieve details of users, system usage, groups, and also to authenticate the user. By using some classes, we can easily retrieve the details from the active directory (AD) database. Active Directory with C# is a foremost tool for enterprise networks and for businesses. When designing with web applications which suit for desktop applications to the web to make powerful organizations.

以上がC# を使用した Active Directoryの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。