【C#】以编程方式在IIS7中设置自定义标识应用程序池的用户帐户
当前位置:点晴教程→知识管理交流
→『 技术文档交流 』
在我的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应用程序无法运行,因为它没有足够的权限。更重要的是,对于我来说,某些.NET类(例如System.Security.Cryptography)会因意外错误代码而失败,即使我手动将此新用户帐户的读/写权限设置为安装了我的Web应用程序的文件系统文件夹。 该文章在 2021/5/11 16:03:35 编辑过 |
关键字查询
相关文章
正在查询... |