2014-05-14 136 views
1

我也跟着下面的文章:Appcmd.exe的無法讀取配置文件,由於權限不足

Cannot read configuration file due to insufficient permissions

我也嘗試手動設置權相關文件,但我不能讓sucess spely。在appcmd.exe文件上。

我試圖更新applicationHost.config文件來動態設置IIS重置時間,當我的網站加載在IIS上。爲此,我試圖在我的項目的global.asax文件中執行以下代碼。

以下是代碼:

string WindowsDir = Server.MapPath("~/Startup"); 
      string command = WindowsDir + @"\Startup.cmd"; 
      string outputFilePath = WindowsDir + @"\log.txt"; 
      string arguments = String.Format("/c echo Startup task (Startup.cmd) executed at {0} >>\"{1}\"", System.DateTime.UtcNow.ToString(), outputFilePath); 
      //System.Diagnostics.Process.Start(command, arguments); 

      System.Diagnostics.Process process = new System.Diagnostics.Process(); 
      System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); 
      startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; 
      startInfo.FileName = command; 
      startInfo.Arguments = arguments; 

      process.StartInfo = startInfo; 
      process.Start(); 

這工作完全當我運行我在Visual Studio中的網站。但是當我將它部署IIS它會產生以下錯誤:

錯誤:

c:\windows\system32\inetsrv>REM *** Prevent the IIS app pools from shutting down due to being idle. 

    c:\windows\system32\inetsrv>C:\Windows\system32\inetsrv\appcmd set config -section:applicationPools -applicationPoolDefaults.processModel.idleTimeout:00:00:00 /commit:apphost 
    ERROR (message:Configuration error 
    Filename: redirection.config 
    Line Number: 0 
    Description: Cannot read configuration file due to insufficient permissions.) 

    c:\windows\system32\inetsrv>REM *** Schedule IIS app pool recycles for 8:00 AM UTC time (1:00 AM MST) 

    c:\windows\system32\inetsrv>C:\Windows\system32\inetsrv\appcmd set config -section:applicationPools -applicationPoolDefaults.recycling.periodicRestart.schedule.[value='08:00:00']" /commit:apphost 
    ERROR (message:Configuration error Filename: redirection.config 
    Line Number: 0 
    Description: Cannot read configuration file due to insufficient permissions.) 

    c:\windows\system32\inetsrv>REM *** Prevent IIS app pool recycles from recycling on the default schedule of 1740 minutes (29 hours). 

    c:\windows\system32\inetsrv>C:\Windows\system32\inetsrv\appcmd set config -section:applicationPools -applicationPoolDefaults.recycling.periodicRestart.time:00:00:00 /commit:apphost 
    ERROR (message:Configuration error 
    Filename: redirection.config 
    Line Number: 0 
    Description: Cannot read configuration file due to insufficient permissions.) 

然後我試圖執行以下代碼:

string WindowsDir = Server.MapPath("~/Startup"); 
      string command = WindowsDir + @"\Startup.cmd"; 
      string outputFilePath = WindowsDir + @"\log.txt"; 
      string arguments = String.Format("/c echo Startup task (Startup.cmd) executed at {0} >>\"{1}\"", System.DateTime.UtcNow.ToString(), outputFilePath); 
      //System.Diagnostics.Process.Start(command, arguments); 

      System.Diagnostics.Process process = new System.Diagnostics.Process(); 
      System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); 
      startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; 
      startInfo.FileName = command; 
      startInfo.Arguments = arguments; 

      string name = "Administrator"; 
      string pass = "password"; 

      startInfo.Password = pass.Aggregate(new System.Security.SecureString(), (ss, c) => { ss.AppendChar(c); return ss; }); 
      startInfo.UserName = name; 
      startInfo.UseShellExecute = false; 
      startInfo.RedirectStandardOutput = true; 

      process.StartInfo = startInfo; 
      process.Start(); 

那麼它並沒有從Visual Studio甚至工作,然後我恢復到在Visual Studio中工作的代碼(如上所述)。而在Web.config文件(內標籤)添加以下標籤:

<authentication mode="Windows"/> 
<identity impersonate="true" userName="Administrator" password="password"/> 

但是,當我跑IIS網站(7.5),沒有工作。

任何想法如何使這項工作在IIS(7.5)?

感謝

回答

0

我有同樣的問題(我用的是IIS 8,Windows 8中,C#,VS 2013),通過代碼有時你have'not權限。我正在執行:

string windir = Environment.GetEnvironmentVariable("windir");  
string comando = windir +"\\System32\\inetsrv\\appcmd.exe set site /site.name:test /+bindings.[protocol='http',bindingInformation='*:80:mitest']"; 

System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + comando); 

而且我得到的錯誤:「無法讀取配置文件,由於權限不足」

在結束使用ServerManager的一流,我在引用這個dll我項目:

C:\ WINDOWS \ SYSTEM32 \ INETSRV \ Microsoft.Web.Administration.dll

然後我使用代碼來操縱AppPools,Sites或綁定:

using (ServerManager serverManager = new ServerManager()) 
{ 
    if (serverManager.ApplicationPools == null) 
     return; 

    for (int i = 0; i < serverManager.Sites.Count; i++) 
    { 
     Microsoft.Web.Administration.ApplicationPoolCollection appPoolCollection = serverManager.ApplicationPools[i]; 
    } 

    if (serverManager.Sites == null) 
     return; 

    for (int i = 0; i < serverManager.Sites.Count; i++) 
    { 
     Microsoft.Web.Administration.BindingCollection bindingCollection = serverManager.Sites[i].Bindings; 
    } 
} 
相關問題