2015-04-22 41 views
0

如何從應用程序ASP.NET MVC運行/執行作業? 我有一個用talend創建的e作業:這個工作將數據從一個數據庫遷移到另一個數據庫。我希望在應用程序asp.net mvc中部署這個工作。 對此有何想法?Talend ASP.NET MVC

+0

您可以運行命令行批處理文件.bat或從asp.net的MVC相似?如果是的話,那麼你可以導出talend作業作爲批處理作業並運行它.. – garpitmzn

回答

0

你可以運行命令行批處理文件.bat或類似的asp.net mvc嗎?如果是,那麼你就可以了Talend作業導出爲批處理作業並運行它

+0

我很肯定,你通常不允許從IIS Web應用程序與標準的IIS用戶做到這一點。 –

0

授予到Web應用程序的文件系統級訪問總是有風險的業務:),但有時會發生。以下是您需要的內容

  1. 一個單獨的技術用戶 - 本地用戶或AD - 可以正確訪問存儲導出的Talend作業的文件夾。我有點不確定正確的訪問權限,首先是因爲編譯的java作業與EXE不完全相同,只能由java執行,其次因爲它也可能取決於你在工作中的作用。但可以通過幾次嘗試來澄清,從讀取和寫入開始。 (從技術上講,你可以使用IIS運行在您的網站上有,但這是不太安全的用戶這樣做。)
  2. 搜索模擬的解釋或代碼樣本(我會提供一個太)。您可以使用模擬來更改運行代碼的環境,從IIS用戶到上面的技術用戶。因爲你在代碼中允許做什麼取決於用戶是誰。
  3. 執行程序流程如下:模擬技術用戶 - >通過調用Talend生成的批處理執行talend作業 - >撤消模擬。

系統調用:

#region accountManagement 

[DllImport("advapi32.dll")] 
public static extern int LogonUserA(String lpszUserName, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken); 
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
public static extern int DuplicateToken(IntPtr hToken, int impersonationLevel, ref IntPtr hNewToken); 
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
public static extern bool RevertToSelf(); 
[DllImport("kernel32.dll", CharSet = CharSet.Auto)] 
public static extern bool CloseHandle(IntPtr handle); 

WindowsImpersonationContext impersonationContext; 

private bool impersonateValidUser(String userName, String domain, String password) 
{ 
    const int LOGON32_LOGON_INTERACTIVE = 2; 
    const int LOGON32_LOGON_NETWORK = 3; 
    const int LOGON32_LOGON_BATCH = 4; 
    const int LOGON32_LOGON_SERVICE = 5; 
    const int LOGON32_LOGON_UNLOCK = 7; 
    const int LOGON32_LOGON_NETWORK_CLEARTEXT = 8; 
    const int LOGON32_LOGON_NEW_CREDENTIALS = 9; 
    const int LOGON32_PROVIDER_DEFAULT = 0; 

    WindowsIdentity tempWindowsIdentity; 
    IntPtr token = IntPtr.Zero; 
    IntPtr tokenDuplicate = IntPtr.Zero; 

    if (RevertToSelf()) 
    { 
     // Int32 result = LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref token); 
     // Response.Write(">>> " + result.ToString()); 
     if (LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref token) != 0) 
     { 
      if (DuplicateToken(token, 2, ref tokenDuplicate) != 0) 
      { 
       tempWindowsIdentity = new WindowsIdentity(tokenDuplicate); 
       impersonationContext = tempWindowsIdentity.Impersonate(); 
       if (impersonationContext != null) 
       { 
        CloseHandle(token); 
        CloseHandle(tokenDuplicate); 
        return true; 
       } 
      } 
     } 
    } 

    if (token != IntPtr.Zero) 
     CloseHandle(token); 
    if (tokenDuplicate != IntPtr.Zero) 
     CloseHandle(tokenDuplicate); 
    return false; 
} 

private void undoImpersonation() 
{ 
    impersonationContext.Undo(); 
} 

#endregion 

,你將不得不自己實現什麼:

  String iuUser = System.Configuration.ConfigurationManager.AppSettings["domain.accountop.user"]; 
      String iuPass = System.Configuration.ConfigurationManager.AppSettings["domain.accountop.pass"]; 
      String iuDomn = System.Configuration.ConfigurationManager.AppSettings["domain.name"]; 
      if (impersonateValidUser(iuUser, iuDomn, iuPass)) 
      { 
       try 
       { 
        // Execute your job here 
        ... 
        // finally: undo impersonation 
        undoImpersonation(); 
        // maybe inform the user that it was successful 
       } 
       catch (Exception ex) 
       { 
        Exception innerException = ex.InnerException; 
        // give some nice error message here, make a log entry, etc 
       } 
      } 
      else 
      { 
       // The impersonation failed. Include a fail-safe mechanism here. 
       // number of possible errors will get you here: wrong or expired password, user does not have a privilege to start an interactive session, etc. 
      }