IIS应用程序池回收
我必须使用 C# 以编程方式回收 IIS(版本 7.5)应用程序池。当我尝试使用“ServerManager”类获取 ApplicationPoolCollection 时引发 UnauthorizedAccessException 。 我想我必须使用模仿。但我不知道如何使用模拟。无论如何,是否可以使用带有属性的模拟来代替 web.config?
I have to recycle IIS (version 7.5) application pool programatically using c#. When I try to get ApplicationPoolCollection using "ServerManager" class UnauthorizedAccessException raised.
I guess I have to use impersonation. But I don't know to use impersonation. And is there anyway to use impersonation with attribute instead of web.config?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要在 IIS 中执行任何管理操作,例如回收应用程序池、启动和停止站点、创建网站等,您需要以管理员身份运行。
如果您想以编程方式模拟用户,则需要使用
WindowsIdentity.Impersonate
方法。这里有一段相当不错的示例代码片段可以帮助您入门。
当然,当涉及到在
web.config
文件(或硬编码)中存储高权限用户的凭据时,所有常见的注意事项都适用于此。另外,如果您尝试使用托管
ServerManager
API 调试以编程方式访问 IIS 的代码,请确保通过执行“以管理员身份运行”来启动 Visual Studio。即使您自己的 Windows 帐户是管理员组的成员,您也需要执行此操作。To perform any administrative operations in IIS such as recycling application pools, starting and stopping sites, creating websites and so on, you need to run as administrator.
If you want to programmatically impersonate a user then you need to use the
WindowsIdentity.Impersonate
method.There's a reasonably good snippet of sample code there to get you started.
Of course all the usual caveats apply here when it comes to storing credentials for highly privileged users in your
web.config
file (or hard coded).Also if you're trying to debug code that accesses IIS programmatically using the managed
ServerManager
API then make sure you start Visual Studio by doing "Run As Administrator". You need to do this even if your own Windows account is a member of the Administrators group.