【问题标题】:Programmatically set up user account for custom identity application pool in IIS 7以编程方式为 IIS 7 中的自定义身份应用程序池设置用户帐户
【发布时间】:2013-10-12 17:19:32
【问题描述】:

在我的 C# 代码中,我需要为我的 Web 应用程序创建一个自定义标识并将其添加到 IIS 7。我执行以下操作:

string strAppPoolName = "MyAppPool";
string strUserName = Environment.UserDomainName + "\\" + "myappusername";

addUserAccount(strUserName, strUserPass);

using (ServerManager serverManager = new ServerManager())
{
    //Add application pool
    ApplicationPool appPool = serverManager.ApplicationPools.Add(strAppPoolName);
    appPool.AutoStart = true;

    appPool.ManagedPipelineMode = ManagedPipelineMode.Integrated;
    appPool.ManagedRuntimeVersion = "v4.0";

    appPool.ProcessModel.MaxProcesses = 1;

    //Assign identity to a custom user account
    appPool.ProcessModel.IdentityType = ProcessModelIdentityType.SpecificUser;
    appPool.ProcessModel.UserName = strUserName;
    appPool.ProcessModel.Password = strUserPass;
}

用户添加到 Active Directory 的位置如下:

public static void addUserAccount(string sUserName, string sPassword)
{
    using (PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain))
    {
        using (UserPrincipal up = new UserPrincipal(oPrincipalContext))
        {
            up.SamAccountName = sUserName;
            up.SetPassword(sPassword);
            up.Enabled = true;
            up.PasswordNeverExpires = true;
            up.Description = "My app's user account";

            up.Save();
        }
    }
}

问题是当我稍后将我的站点和应用程序添加到该应用程序池下的 IIS 7 时,Web 应用程序无法运行,因为它没有足够的权限。对我来说更重要的是,即使我手动将此新用户帐户的读/写权限设置为安装我的 Web 应用程序的文件系统文件夹,一些 .NET 类(例如 System.Security.Cryptography)也会失败并出现意外错误代码。

所以在进行研究时,我发现了following statement

如果您使用自定义身份,请确保您使用的用户帐户 指定是 Web 服务器上 IIS_IUSRS 组的成员,以便 该帐户具有对资源的适当访问权限。此外,当您使用 Windows 和 Kerberos 身份验证在您的环境中,您可能 需要向域注册服务主体名称 (SPN) 控制器 (DC)。

那么,你是怎么做到的呢?

【问题讨论】:

标签: c# asp.net .net security iis


【解决方案1】:

如果您需要将该帐户添加到 IIS_IUSERS 组(在计算机上是本地的),您可以使用GroupPrincipal。请记住为您的机器创建一个本地的PrincipalContext,而不是您为用户使用的域。您可以简单地按身份找到组,然后将新创建的用户添加到Memberscollection。 Add 方法有一个接受UserPrincipal 的重载。

你的代码应该是这样的:

using (PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain))
{
    using (PrincipalContext oGroupContext = new PrincipalContext(ContextType.Machine))
    {
        // find the local group IIS_IUSRS
        using(var gp = GroupPrincipal.FindByIdentity(oGroupContext,"IIS_IUSRS"))
        {
            using (UserPrincipal up = new UserPrincipal(oPrincipalContext))
            {
                up.SamAccountName = sUserName;
                up.SetPassword(sPassword);
                up.Enabled = true;
                up.PasswordNeverExpires = true;
                up.Description = "My app's user account";

                up.Save();

                // add new user to Members of group
                gp.Members.Add(up);
                // save before Disposing!
                gp.Save();
            }
         }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-12
    • 2010-11-30
    • 2011-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-01
    相关资源
    最近更新 更多