如何从 C# 更改 IIS 中应用程序池的用户名/密码?

发布于 2024-07-15 03:31:41 字数 961 浏览 5 评论 0原文

我使用下面的代码在应用程序的 Installer 类中创建一个新的应用程序池:

private static void CreateAppPool(string serverName, string appPoolName)
{
    //  metabasePath is of the form "IIS://<servername>/W3SVC/AppPools"
    //    for example "IIS://localhost/W3SVC/AppPools" 
    //  appPoolName is of the form "<name>", for example, "MyAppPool"
    string metabasePath = string.Format("IIS://{0}/W3SVC/AppPools", serverName);
    Console.WriteLine("\nCreating application pool named {0}/{1}:", metabasePath, appPoolName);
    try
    {
        DirectoryEntry apppools = new DirectoryEntry(metabasePath);
        DirectoryEntry newpool = apppools.Children.Add(appPoolName, "IIsApplicationPool");
        newpool.CommitChanges();
        Console.WriteLine("AppPool created.");
    }
    catch (Exception ex)
    {
        Console.WriteLine("Failed in CreateAppPool with the following exception: \n{0}", ex.Message);
    }
}

如何更改运行此应用程序池的用户凭据?

I am using the code below to create a new application pool in the Installer class of my application:

private static void CreateAppPool(string serverName, string appPoolName)
{
    //  metabasePath is of the form "IIS://<servername>/W3SVC/AppPools"
    //    for example "IIS://localhost/W3SVC/AppPools" 
    //  appPoolName is of the form "<name>", for example, "MyAppPool"
    string metabasePath = string.Format("IIS://{0}/W3SVC/AppPools", serverName);
    Console.WriteLine("\nCreating application pool named {0}/{1}:", metabasePath, appPoolName);
    try
    {
        DirectoryEntry apppools = new DirectoryEntry(metabasePath);
        DirectoryEntry newpool = apppools.Children.Add(appPoolName, "IIsApplicationPool");
        newpool.CommitChanges();
        Console.WriteLine("AppPool created.");
    }
    catch (Exception ex)
    {
        Console.WriteLine("Failed in CreateAppPool with the following exception: \n{0}", ex.Message);
    }
}

How can I change the user credentials under which this application pool is running?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怎会甘心 2024-07-22 03:31:41

将以下内容添加到创建 newpool 的代码行之后:

DirectoryEntry newpool = 
            apppools.Children.Add(appPoolName, "IIsApplicationPool");
// Add this:
newpool.Properties["AppPoolIdentityType"].Value = 3;
newpool.Properties["WAMUserName"].Value = 
            Environment.MachineName + @"\" + username;
newpool.Properties["WAMUserPass"].Value = password;

显然,您需要将字符串变量 usernamepassword 添加到您的 CreateAppPool() 方法参数也是如此。

如果您还不知道,您需要做的另一件事是确保您的应用程序池用户获得足够的权限来访问 IIS 元数据库、ASP.NET 临时文件夹等。您可以通过运行以下命令来完成此操作:

aspnet_regiis.exe -ga <username>

您可以在文件夹 %SYSTEMROOT%\Microsoft.NET\Framework\v2.0.50727 中找到此工具。 我通常只是使用 System.Diagnostics.Process 进行处理。

最后,应用程序池用户(至少)需要对应用程序的 Web 文件夹的读取权限。

Add the following to your code just after the line where you create newpool:

DirectoryEntry newpool = 
            apppools.Children.Add(appPoolName, "IIsApplicationPool");
// Add this:
newpool.Properties["AppPoolIdentityType"].Value = 3;
newpool.Properties["WAMUserName"].Value = 
            Environment.MachineName + @"\" + username;
newpool.Properties["WAMUserPass"].Value = password;

You'll obviously need to add the string variables username and password to your CreateAppPool() method parameters as well.

Another thing you need to do, if you weren't already aware, is make sure your application pool user gets sufficient rights to access the IIS metabase, ASP.NET temp folders etc. You can do this by running the following command:

aspnet_regiis.exe -ga <username>

You can find this tool in the folder %SYSTEMROOT%\Microsoft.NET\Framework\v2.0.50727. I usually just shell out using System.Diagnostics.Process.

And finally, the application pool user will need (at least) read rights on the web folder for the app.

戏蝶舞 2024-07-22 03:31:41
aspnet_regiis.exe -ga <username>

可能不是最好使用的命令。

您应该将 APPPool 用户添加到 IIS_WPG 组并授予使用该组的权限。

aspnet_regiis.exe -ga <username>

Is probably not the best command to use.

You should add the APPPool user to the IIS_WPG group and grant rights using that group.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文