【问题标题】:IIS7 programmatically modify application anonymous accessIIS7 以编程方式修改应用程序匿名访问
【发布时间】:2013-12-01 21:26:43
【问题描述】:

我正在尝试使用以下代码在 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

【问题讨论】:

    标签: c# iis iis-7 anonymous-access


    【解决方案1】:

    上述代码无效,因为出于安全原因,该部分已锁定在 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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-09
      • 1970-01-01
      相关资源
      最近更新 更多