【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 編輯過 |
關鍵字查詢
相關文章
正在查詢... |