6

学習目的で、私は独自の Active Directory クライアントを作成しています。アカウントのロック解除やパスワードのリセットなどのコマンドを実行するには、管理者アカウントとパスワードを入力しWindowsIdentity.Impersonate、管理者アカウントとしてコードを使用して実行する必要があります。プログラム全体でパスワードを何度も使用する必要があるため、パスワードを保護するためのオプションについて疑問に思っています。

私が理解していることSecureStringから、パスワードを保存するために使用できます。しかし、管理者アカウントとしてコードを実行するには、 を使用しており、それを使用するには、 ではなく通常のトークンを必要とするWindowsIdentity.Impersonateトークンを取得する必要があります。LogonUserstringSecureString

だから私はしなければならないでしょう:

  1. 起動時にログイン

  2. 入力をSecureString

  3. 入力をクリア

その後、昇格を必要とする関数を実行したい場合:

  1. 以前に作成SecureStringしたものをstring

  2. 変換された文字列をLogonUser

  3. 変換文字列をクリアしますnull

  4. コマンド実行

  5. LogonUserオブジェクトをクリア

これはこれにアプローチする正しい方法ですか?SecureStringを使用するために変換する必要があるのは奇妙に思えstringます...目的を無効にし、変換中にパスワードをより脆弱なままにしてしまうようです。

編集: LogonUser の名前を修正

4

1 に答える 1

7

クラスで、パスワードの代わりに使用UserImpersonationする宣言方法を変更します。LogonUserIntPtrstring

Marshal.SecureStringToGlobalAllocUnicodeのコード サンプルには、まさに必要なものが含まれています。関連するスニペットは次のとおりです。

[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern bool LogonUser(String username, String domain,
                                      IntPtr password, int logonType,
                                      int logonProvider, ref IntPtr token);

...

IntPtr tokenHandle = IntPtr.Zero;

IntPtr passwordPtr = IntPtr.Zero;

try
{
    // Marshal the SecureString to unmanaged memory.
    passwordPtr = Marshal.SecureStringToGlobalAllocUnicode(password);

    returnValue = LogonUser(userName, domainName, passwordPtr,
                            LOGON32_LOGON_INTERACTIVE,
                            LOGON32_PROVIDER_DEFAULT, ref tokenHandle);

 }
 finally
 {
     // Zero-out and free the unmanaged string reference.
     Marshal.ZeroFreeGlobalAllocUnicode(passwordPtr);

     // Close the token handle.
     CloseHandle(tokenHandle);
 }
于 2015-01-08T23:39:53.133 に答える