7

ユーザーに提供したばかりの新しいユーザー作成アプリケーションがあります。ただし、これらのユーザーは、ユーザーを作成する権限を持っていなくても、アプリケーションを介してユーザーを作成できる必要があります。

C# では、この機能を実現するために別のユーザーになりすます方法を教えてください。このアプリケーションは主System.DirectoryServicesに .

コードスニペット:

DirectoryEntry dEntry = new DirectoryEntry("LDAP://OU=");
DirectorySearcher dSearcher = new DirectorySearcher(dEntry);
//filter just user objects
dSearcher.SearchScope = SearchScope.Subtree;
dSearcher.Filter = "(&(objectClass=user)(mail=" + excel_Holding_Table.Rows[i]["EmailAddress"].ToString() + "))";
dSearcher.PageSize = 1000;
sResults = dSearcher.FindAll();
4

4 に答える 4

10

クラスを直接使用してDirectoryEntry、ユーザー名とパスワードを指定できます。

DirectoryEntry de = new DirectoryEntry(path);

de.Username = "username";
de.Password = "password";

そして、de オブジェクトから Active Directory にアクセスします。または、WindowsIdentityクラスを使用して、ユーザーになりすますことができます。

WindowsIdentity newId = new WindowsIdentity(safeTokenHandle.DangerousGetHandle());
WindowsImpersonationContext impersonatedUser = newId.Impersonate();

完全なコード サンプルは、次の場所で入手できます。

偽装と DirectoryEntry

于 2012-05-24T17:50:25.660 に答える
0

ユーザー名、パスワード、および authenticationType パラメーターを受け取るコンストラクターを使用DirectoryEntryます。

余談ですが、DirectoryEntry DirectorySearcherandSearchResultCollection型は、おそらくステートメントIDisposableで破棄する必要があります。using

于 2012-05-24T17:51:25.617 に答える
0

偽装の代わりにユーザー名とパスワードを取る DirectoryEntry コンストラクター (文字列、文字列、文字列、AuthenticationTypes) を使用します。

DirectoryEntry directoryEntry = new DirectoryEntry("IIS://" + serverName + "/W3SVC/1/Root", @"domain\username", "password", AuthenticationTypes.Secure | AuthenticationTypes.Sealing); 

参照

于 2012-05-24T17:52:20.760 に答える