授予网络服务权限以回收iis应用程序池

时间:2011-05-19 15:27:19

标签: c# asp.net iis-6 windows-server-2003

我正在尝试创建一个.NET Web应用程序,它在远程Web服务器上重新启动应用程序池,在Windows Server 2003上运行IIS 6.0。我有代码可以工作,但我有权限问题。

string appPoolPath = ConfigurationSettings.AppSettings["ApplicationPool"];

string systemId = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

try
{

    DirectoryEntry svc = new DirectoryEntry(appPoolPath);
    svc.Invoke("Recycle");
    LabelResult.Text = "Application Pool Recycled Succesfully!";
    LabelResult.Visible = true;
}
catch(Exception exc)
{

    LabelResult.Text = "Error (" + systemId + "): " + exc.Message + " : " + exc.InnerException;

    LabelResult.Visible = true;
}

当我运行代码时,出现以下错误:

错误(NT AUTHORITY \ NETWORK SERVICE):调用目标抛出了异常。 :System.UnauthorizedAccessException:访问被拒绝。 (HRESULT异常:0x80070005(E_ACCESSDENIED))

所以我的问题是,如何在不给予帐户完全管理员权限的情况下授予NETWORK SERVICE帐户调用回收的权限?有可能吗?

我知道解决此问题的另一种方法是冒充服务器上的一个现有管理员,但我不允许这样做。我无法在计算机上创建用户,也无法获取现有用户帐户的登录凭据。

3 个答案:

答案 0 :(得分:1)

另一种选择:

将一个虚拟文件放在/ bin目录中,并在每次要重新启动池时更新它

答案 1 :(得分:1)

这不完全正确。 你需要Web.Config中的一节:

<section name="TestSection" restartOnExternalChanges="true" requirePermission="false" type="System.Configuration.AppSettingsSection, System.Configuration"/>

<TestSection configSource="Test.config"></TestSection>

然后,当您修改Test.config时,应用程序池将被回收。

答案 2 :(得分:0)

尝试删除并创建应用程序池。已经解决了我的问题。

var server = new ServerManager();
var pool = server.ApplicationPools.FirstOrDefault(q => q.Name == "MyPool");
if (pool != null)
{
    server.ApplicationPools.Remove(pool);
    server.CommitChanges();

    server.ApplicationPools.Add("MyPool");
    server.CommitChanges();
}