[英]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.