2011-09-07 73 views
4

我嘗試更改Windows Azure上應用程序池的標識。在使用Windows Azure時,我的項目使用此應用程序池。默認情況下,應用程序池使用NetworkService標識,但我必須使用其他標識。我試圖通過這種方式改變它在OnStart()事件WebRole的:更改應用程序池標識時出錯

using (ServerManager serverManager = new ServerManager()) 
{      
    string appPoolName = 
    serverManager.Sites[RoleEnvironment.CurrentRoleInstance.Id + "_Web"] 
    .Applications.First().ApplicationPoolName; 

    var appPool = serverManager.ApplicationPools[appPoolName]; 

    appPool.ProcessModel.UserName = Environment.MachineName + "\\UserName"; 

    appPool.ProcessModel.IdentityType = ProcessModelIdentityType.SpecificUser; 

    appPool.ProcessModel.Password = "UserPassword"; 

    serverManager.CommitChanges(); 
} 

,但我得到的異常與下一條消息:

System.Runtime.InteropServices.COMException (0x80090016): 
     Keyset does not exist (Exception from HRESULT: 0x80090016) 
    at Microsoft.Web.Administration.Interop.AppHostWritableAdminManager.CommitChanges() 
    at Microsoft.Web.Administration.Configuration.CommitChanges() 
    at Microsoft.Web.Administration.ConfigurationManager.CommitChanges() 
    at Microsoft.Web.Administration.ServerManager.CommitChanges() 
    at Project.Web.WebRole.OnStart() in E:\Projects\...\Web\WebRole.cs:line 57 

如果我在IIS管理器更改身份我沒有得到任何錯誤。我的代碼出了什麼問題,爲什麼會出現此錯誤?

+0

分支和綁定和分離的擔憂。你可以首先嚐試調試,並只應用一個小的改變,看看CommitChanges是否工作或仍然失敗?註釋掉你的身份代碼,看看你是否設法改變另一個屬性... –

回答

2

對applicationHost.config的更新需要管理權限。當您在本地運行時,您是管理員。在雲中,除非您提升角色,否則您的RoleEntryPoint將以普通用戶身份運行。你有沒有這樣做?

檢查您是否在您的角色聲明的ServiceDefinition.csdef中指定了<Runtime executionContext="elevated"/>

編輯:韋德還展示瞭如何使用稍微不同的方法來做到這一點(檢查註釋)。 Try this as well

+0

謝謝!我試過這樣做http://www.wadewegner.com/2011/01/programmatically-changing-the-apppool-identity-in-a-windows-azure-web-role/但我得到了另一個例外 –