2011-07-28 27 views
5

我是專家程序員,所以因此,我沒有一個線索,WTH我做:)C#服務狀態在遠程計算機

在一個嚴重的注意;不,我不是專家。我確實有一個問題,但不知道如何解決它。好的是,我(想我)知道問題是什麼,我希望這裏有人能夠提供幫助。

下面是問題的簡介。我正在C#中創建一個表單,它將爲我執行一些服務器和數據庫管理任務。我有一個按鈕,當點擊時應該返回「y」服務器上的「x」服務的服務狀態。狀態在屏幕上打印到文本框中。

這裏是我的代碼:

 private void button2_Click(object sender, EventArgs e) 
    { 
     string fs = "Service X Status = "; 
     string mr = "Service A Status = "; 
     string qp = "Service B Status = "; 
     string sp = "Spooler Service Status = "; 
     ServiceController fssc = new ServiceController("xService", "yServer"); 
     ServiceController mrsc = new ServiceController("aService", "yServer"); 
     ServiceController qpsc = new ServiceController("bService", "yServer"); 
     ServiceController spsc = new ServiceController("Spooler", "yServer"); 

     try 
     { 
      txtGtwySts.AppendText(sp + spsc.Status.ToString()); 
      txtGtwySts.AppendText(Environment.NewLine); 
      txtGtwySts.AppendText(fs + fssc.Status.ToString()); 
      txtGtwySts.AppendText(Environment.NewLine); 
      txtGtwySts.AppendText(mr + mrsc.Status.ToString()); 
      txtGtwySts.AppendText(Environment.NewLine); 
      txtGtwySts.AppendText(qp + qpsc.Status.ToString()); 
     } 
     catch (Exception crap) 
     { 
      string msg = ""; 
      int i; 
      for (i = 0; i < crap.Message.Count(); i++) 
      { 
       msg += "Error # " + i + " Message: " + crap.Message + "\n"; 

      } 
      MessageBox.Show(msg); 
      MessageBox.Show(i.ToString()); 
     } 
    } 

我得到的例外,基本上是說:「服務器」不能上打開「服務」由於這是一個遠程服務器,我假設這是一個憑證/安全問題。但是,我不會對後臺打印程序服務有任何問題。

我的問題是......如何將用戶ID和密碼傳遞給此服務器,以便它將進行身份驗證或運行,以便我可以檢查這些服務的狀態,這就是問題所在。如果人如果沒有認爲它的問題,那麼請告訴我,我已經出了問題:)

+0

OK,我發現怎麼牛逼o這樣做,但我不太懂如何編碼。從我讀過的內容來看,我需要在一些模擬userID的代碼中包裝我想要做的事情,然後在得到所需信息之後解除模擬。我明白,對用戶信息進行硬編碼並不是最安全的方式,但它足以滿足這些要求。我將繼續研究如何在C#中模仿。 – Jason

回答

10

終於找到它了...

創建一個新的類,並顯示如下:

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]"); 
// code you want to execute as impersonated user..... 
iu.Undo(); 
+1

這是否只適用於所有連接到域控制器的環境? –

+0

只有在它們之間建立了信任關係時,這才能跨域使用。 –