简体   繁体   English

IIS7以编程方式修改应用程序匿名访问

[英]IIS7 programmatically modify application anonymous access

I'm trying to enable anonymous access on an application in IIS7, using the code below: 我正在尝试使用以下代码在IIS7中的应用程序上启用匿名访问:

ConfigurationSection config = server.GetWebConfiguration(webSiteName).GetSection("system.webServer/security/authentication/anonymousAuthentication", "/" + applicationName);
config.OverrideMode = OverrideMode.Allow;
config["enabled"] = true;

However I'm getting this error: 但是我收到此错误:

Failed: The request is not supported. (Exception from HRESULT: 0x80070032)

How can I modify anonymous access for the application? 如何修改应用程序的匿名访问?

Thanks, ng93 谢谢,ng93

The code above is invalid since the section is locked at ApplicationHost.config level for security reasons. 由于该部分出于安全原因而被锁定在ApplicationHost.config级别,因此上面的代码无效。 In the code you are trying to use it is trying to set it in Web.config. 在您尝试使用的代码中,尝试在Web.config中进行设置。 If you really wanted that you would first need to request it from a GetApplicationHost call set the overrideMode and then get the section again from a GetWebConfiguration. 如果确实需要,则首先需要从GetApplicationHost调用中请求它,设置overrideMode,然后再次从GetWebConfiguration获取该部分。 But all in all I would still recommend instead setting that value at the server level so that it cannot be accidentally changed in web.config by a deployment or some other mechanism. 但是总而言之,我仍然建议改为在服务器级别上设置该值,以免部署或其他机制在web.config中意外更改该值。

So to do that I would instead recommend doing: 因此,我建议您这样做:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM