我正在尝试使用以下代码在 IIS7 中的应用程序上启用匿名访问:
ConfigurationSection config = server.GetWebConfiguration(webSiteName).GetSection("system.webServer/security/authentication/anonymousAuthentication", "/" + applicationName);
config.OverrideMode = OverrideMode.Allow;
config["enabled"] = true;
但是我收到了这个错误:
Failed: The request is not supported. (Exception from HRESULT: 0x80070032)
如何修改应用程序的匿名访问权限?
谢谢, ng93
最佳答案
上面的代码无效,因为出于安全原因,该部分在 ApplicationHost.config 级别被锁定。在您尝试使用的代码中,它试图在 Web.config 中进行设置。如果您真的想要,您首先需要从 GetApplicationHost 调用请求它,设置 overrideMode,然后再次从 GetWebConfiguration 获取该部分。但总而言之,我仍然建议在服务器级别设置该值,这样它就不会在 web.config 中被部署或其他机制意外更改。
因此,我建议您这样做:
string webSiteName = "Default Web Site";
string applicationName = "MyApp";
using (ServerManager server = new ServerManager())
{
ConfigurationSection config = server.GetApplicationHostConfiguration()
.GetSection(@"system.webServer/security/
authentication/
anonymousAuthentication",
webSiteName + "/" + applicationName);
config.OverrideMode = OverrideMode.Allow;
config["enabled"] = true;
server.CommitChanges();
}
关于c# - IIS7 以编程方式修改应用程序匿名访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20052705/