[英]IIS7 programmatically modify application anonymous access
我正在尝试使用以下代码在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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.