2016-06-29 3 views
0

リモートでサービスを停止して開始できるアプリケーションを実行しようとしています。アプリケーションは、ユーザー名、パスワード、およびドメインが暗号化されたデータベースから読み取られるユーザーになりすますことによって、管理者特権なしでユーザーが使用できる必要があります。wpfアプリケーションで現在のユーザー資格情報をデバッグモードで確認してください。

私が現在直面している問題は、偽装機能が正しく動作しているかどうかわからないということです。なぜ、ユーザーが最新のデバッグモードでチェックしたいのですか?

これは私がユーザーを偽装するために使用するクラスです:

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Security.Principal; 
using System.Runtime.InteropServices; 
using System.Security.Permissions; 

public class ImpersonateUser 
{ 
    [DllImport("advapi32.dll", SetLastError = true)] 
    public static extern bool LogonUser(
    String lpszUsername, 
    String lpszDomain, 
    String lpszPassword, 
    int dwLogonType, 
    int dwLogonProvider, 
    ref IntPtr phToken); 
    [DllImport("kernel32.dll", CharSet = CharSet.Auto)] 
    public extern static bool CloseHandle(IntPtr handle); 
    private static IntPtr tokenHandle = new IntPtr(0); 
    private static WindowsImpersonationContext impersonatedUser; 
    // If you incorporate this code into a DLL, be sure to demand that it 
    // runs with FullTrust. 
    [PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")] 
    public void Impersonate(string domainName, string userName, string password) 
    { 
     //try 
     { 
      // Use the unmanaged LogonUser function to get the user token for 
      // the specified user, domain, and password. 
      const int LOGON32_PROVIDER_DEFAULT = 0; 
     // Passing this parameter causes LogonUser to create a primary token. 
     const int LOGON32_LOGON_INTERACTIVE = 2; 
     tokenHandle = IntPtr.Zero; 
     // ---- Step - 1 
     // Call LogonUser to obtain a handle to an access token. 
     bool returnValue = LogonUser(
     userName, 
     domainName, 
     password, 
     LOGON32_LOGON_INTERACTIVE, 
     LOGON32_PROVIDER_DEFAULT, 
     ref tokenHandle); // tokenHandle - new security token 
     if (false == returnValue) 
     { 
      int ret = Marshal.GetLastWin32Error(); 
      throw new System.ComponentModel.Win32Exception(ret); 
     } 
     // ---- Step - 2 
     WindowsIdentity newId = new WindowsIdentity(tokenHandle); 
     // ---- Step - 3 
     { 
      impersonatedUser = newId.Impersonate(); 
     } 
    } 
} 
// Stops impersonation 
public void Undo() 
{ 
    impersonatedUser.Undo(); 
    // Free the tokens. 
    if (tokenHandle != IntPtr.Zero) 
    { 
     CloseHandle(tokenHandle); 
    }    
}   

}

これは私がそれを使用しようとしている方法です:

ImpersonateUser iu = new ImpersonateUser(); 
iu.Impersonate("[domain]","[username]","[password]"); 

try{ 
    ServiceController service = new ServiceController(serviceName, remoteComputer); 
    service.Start(); 
} 

iu.Undo(); 

しかし、私はありません私が挿入したコードが大丈夫か、私がコードを書いているときに別のものを追加するべきかどうかを確かめてください。

+0

デバッグについて話しているので、[#ifデバッグ](https://msdn.microsoft.com/en-us/library/4y6tbswk.aspx?f=255&MSPPError=-2147217396)を見たことがあります – Athafoud

+0

はい私はそれを見ましたしかし、これは私が必要としたものではありません。私はWindowsidentity.GetCurrent()について知りました。私はこれが私が探していたものだと思います。 – YMIFrozen

答えて

0

私が探しているものが見つかりました.WindowsIdentity.GetCurrent()の簡単な呼び出しです。返り値が現在アプリケーションを使用しているユーザーで、私の場合のようにImpersonate()を使用している場合は、次のように呼び出しにブール値を渡す必要があります。WindowsIdentity.GetCurrent(true).Name; Whihは偽装されたユーザーを返します。

EDIT

また、私は、私は、リモートマシン上のユーザーの権限をチェックしようとしていたので、これはかなりやらなかったのと同じ問題に直面することができる他の人のために何かを追加したいと思いますトリック。私はこのような使用

static bool isAdmin(string username, string machinename) 
{ 
using (PrincipalContext ctxMacine = new PrincipalContext(ContextType.Machine, machinename)) 
{ 
    using (PrincipalContext ctxDomain = new PrincipalContext(ContextType.Domain)) 
    { 
     UserPrincipal up = UserPrincipal.FindByIdentity(ctxDomain, IdentityType.SamAccountName, username); 
     GroupPrincipal gp = GroupPrincipal.FindByIdentity(ctxMacine, "Administrators"); 

     foreach (UserPrincipal usr in gp.GetMembers(true)) 
     { 
      if (up != null) 
      { 
       if (up.SamAccountName.ToUpper() == usr.SamAccountName.ToUpper()) 
       { 
        return true; 
       } 
      } 
     } 
    } 
} 
return false; 

}

別の関数を使用する理由です:あなたは偽装が「リモートコンピュータあなたに管理者権限を持っている現在のユーザーならば

ImpersonateUser iu = new ImpersonateUser(); 
     iu.Impersonate(domain, userName, password); 

string name = WindowsIdentity.GetCurrent(true).Name; 
MessageBox.Show("CurrentUser: " + name + " " + isAdmin(name, remoteComputer)); 

iu.Undo(); 

はあなたを教えてくれますアクセスしようとしています。

関連する問題