我有以下異步控制器操作,它允許我在遠程服務器上啓動控制檯應用程序。我的問題是關於權限和身份驗證。我嘗試了以下,在我的理解中允許app.Start(valuationDate)
作爲模擬用戶運行。原因是控制檯應用程序需要訪問網絡資源,並且此Web應用程序的用戶將擁有所需的訪問權限。 (作爲一個側面說明,控制檯應用程序作爲計劃任務運行,沒有錯誤)使用經過身份驗證的用戶憑據從asp.net啓動控制檯應用程序
我懷疑的問題是控制檯應用程序仍在IIS應用程序池標識下運行,導致網絡資源「訪問被拒絕」錯誤。控制檯應用程序本身啓動,但IIS用戶帳戶的受限訪問導致失敗。
我已經嘗試更改AppPool身份以作爲授權用戶運行,並且該過程正確執行。我希望有一個解決方案不需要我更改服務器上的AppPool標識。
我該如何使用經過驗證的用戶詳細信息啓動控制檯應用程序?
控制器操作:
[HttpPost]
public void RunAsync(DateTime valuationDate)
{
AsyncManager.OutstandingOperations.Increment();
Task.Factory.StartNew(() => RunApp(valuationDate));
}
private void RunApp(DateTime valuationDate)
{
ConsoleWrapper app = new ConsoleWrapper();
WindowsIdentity winId = (WindowsIdentity)HttpContext.User.Identity;
WindowsImpersonationContext ctx = null;
try
{
ctx = winId.Impersonate();
app.Start(valuationDate);
}
catch (Exception e)
{
throw;
}
finally
{
if (ctx != null) {ctx.Undo();}
}
AsyncManager.Parameters["success"] = app.Success();
AsyncManager.Parameters["message"] = app.Message();
AsyncManager.OutstandingOperations.Decrement();
}
ConsoleWrapper:
public void Start(DateTime valuationDate)
{
var appExe = Config.Default.CONSOLE_APP;
InProgress = true;
log = String.Empty;
success = false;
message = String.Empty;
try
{
var process = new Process();
process.StartInfo.FileName = appExe;
process.StartInfo.Arguments = valuationDate.ToString("dd-MMM-yyyy");
process.EnableRaisingEvents = true;
process.StartInfo.UseShellExecute = true;
process.StartInfo.RedirectStandardOutput = true;
process.Exited += new EventHandler(process_Exited);
process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);
process.Start();
process.WaitForExit();
}
catch (Exception e){/* snip */}
}
除代表團外還有其他方式嗎?也許從程序的角度來看? – Ahmad 2011-03-09 08:19:39
@Ahmad,你可以通過編程來完成,但你需要知道客戶端的密碼,我想這不是你的情況。如果您不知道密碼,您需要設置授權。另一種可能性是擁有一些通用帳戶,該帳戶將始終用於運行控制檯應用程序。 – 2011-03-09 08:30:25
謝謝達林,我也這麼想。我可能會堅持域帳戶/ AppPool組合,因爲它似乎是目前最簡單的。我將測試代表團的選擇,但擔心IT部門可能會阻礙進展。 – Ahmad 2011-03-09 08:39:31