更改应用程序池身份时出现错误

4

我尝试为 Windows Azure 上的应用程序池更改身份。我的项目在 Windows Azure 上运行时使用此应用程序池。默认情况下,应用程序池使用 NetworkService 身份,但我必须使用另一个身份。我尝试通过 OnStart() 事件来更改它:

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管理器中更改身份,则不会出现任何错误。我的代码有什么问题,为什么会出现此错误?


分支定界和关注点分离。首先,您可以尝试调试并仅应用小的更改,然后查看CommitChanges是否有效或仍然失败?注释掉身份验证代码并查看是否成功更改其他属性... - Davide Piras
2个回答

2

应用程序配置文件(applicationHost.config)的更新需要管理员权限。当您在本地运行时,您是管理员。在云中,除非您提升角色,否则您的RoleEntryPoint将作为普通用户运行。您这样做了吗?

请检查ServiceDefinition.csdef中的角色声明是否指定了<Runtime executionContext="elevated"/>

编辑: Wade还展示了使用稍微不同的方法来实现此目的(请查看评论)。也可以尝试这个方法


谢谢!我尝试执行这个操作:http://www.wadewegner.com/2011/01/programmatically-changing-the-apppool-identity-in-a-windows-azure-web-role/,但是我遇到了另外一个异常。 - Pavel F

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接