0

プログラムで偽装を使用しています。問題ありません。ただし、Windows サービスを作成すると、偽装中に例外が発生します。何が問題になる可能性がありますか? 私のアカウントでは、偽装を正常に適用できますが、Windows サービスはローカル システム アカウントで実行されます。それって問題ですか?

これが私のコードです:

public enum SECURITY_IMPERSONATION_LEVEL : int
{
    SecurityAnonymous = 0,
    SecurityIdentification = 1,
    SecurityImpersonation = 2,
    SecurityDelegation = 3
}

public static WindowsImpersonationContext ImpersonateUser(string sUsername, string sDomain, string sPassword)
    {
        // initialize tokens
        IntPtr pExistingTokenHandle = new IntPtr(0);
        IntPtr pDuplicateTokenHandle = new IntPtr(0);
        pExistingTokenHandle = IntPtr.Zero;
        pDuplicateTokenHandle = IntPtr.Zero;

        // if domain name was blank, assume local machine
        if (sDomain == "")
            sDomain = System.Environment.MachineName;

        try
        {
            string sResult = null;
            const int LOGON32_PROVIDER_DEFAULT = 0;                
            const int LOGON32_LOGON_INTERACTIVE = 2;

            // get handle to token
            bool bImpersonated = LogonUser(sUsername, sDomain, sPassword,
                LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref pExistingTokenHandle);

            // did impersonation fail?
            if (!bImpersonated)
            {
                //Giriş yapılırken hata ile karşılaşıldı
                Helper.ShowErrorMsg(ErrorAndInfoMessages.ErrorOnLogon);
                return null;
            }

            // Get identity before impersonation
            sResult += "Before impersonation: " + WindowsIdentity.GetCurrent().Name + "\r\n";
            bool bRetVal = DuplicateToken(pExistingTokenHandle, (int)SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation, ref pDuplicateTokenHandle);

            // did DuplicateToken fail?
            if (!bRetVal)
            {
                //DuplicateToken() failed
                Helper.ShowErrorMsg(ErrorAndInfoMessages.ErrorTokenFailed);
                return null;
            }
            else
            {
                // create new identity using new primary token
                WindowsIdentity newId = new WindowsIdentity(pDuplicateTokenHandle);
                WindowsImpersonationContext impersonatedUser = newId.Impersonate();

                // check the identity after impersonation
                sResult += "After impersonation: " + WindowsIdentity.GetCurrent().Name + "\r\n";
                return impersonatedUser;
            }
        }
        catch (Exception ex)
        {
            Helper.ShowErrorMsg("ImpersonateUser Hata: " + ex.Message);
            return null;   
        }
        finally
        {
            // close handle(s)
            if (pExistingTokenHandle != IntPtr.Zero)
                CloseHandle(pExistingTokenHandle);
            if (pDuplicateTokenHandle != IntPtr.Zero)
                CloseHandle(pDuplicateTokenHandle);                
        }
    }

例外は次の とおりです。アプリケーションが UserInteractive モードで実行されていないときにモーダル ダイアログ ボックスまたはフォームを表示することは、有効な操作ではありません。サービス アプリケーションからの通知を表示するには、ServiceNotification または DefaultDesktopOnly スタイルを指定します。

また、自分のアカウントで Windows サービスを実行しようとしましたが、何も変わりませんでした。

4

1 に答える 1

1

LOGON32_LOGON_INTERACTIVE = 2 の代わりに LOGON32_LOGON_NETWORK = 3 を使用してみてください。MSDNによると、 LOGON32_LOGON_INTERACTIVE はコンピューターを対話的に使用するユーザーを対象としているため、Windows サービスのような無人プロセスは失敗する可能性があります。

同じ問題に遭遇し、上記の変更により修正されました。

于 2013-02-20T23:41:21.190 に答える