像@SouthShoreAK我认为不能以这种方式完成,但总是有选择,一种方法可能是拥有一个基本的web.config,您可以在您创建的每个文件夹中编辑一个保存,您可以在其中添加您需要的授权,我放在下面的代码就是这样做的。
try
{
//Load the empty base configuration file
Configuration config = WebConfigurationManager.OpenWebConfiguration("~/WebEmpty.config");
//Get te authorization section
AuthorizationSection sec = config.GetSection("system.web/authorization") as AuthorizationSection;
//Create the access rules that you want to add
AuthorizationRule allowRule = new AuthorizationRule(AuthorizationRuleAction.Allow);
allowRule.Users.Add("userName");
//allowRule.Users.Add("userName2"); Here can be added as much users as needed
AuthorizationRule denyRule = new AuthorizationRule(AuthorizationRuleAction.Deny);
denyRule.Users.Add("*");
//Add the rules to the section
sec.Rules.Add(allowRule);
sec.Rules.Add(denyRule);
//Save the modified config file in the created folder
string path = MapPath("~/NewFolder/Web.config");
config.SaveAs(path);
}
catch (Exception ex)
{
//Handle the exceptions that could appear
}
你的 WebEmpty.config 会是这样的
<?xml version="1.0"?>
<configuration>
</configuration>
你保存的文件看起来像这样
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<authorization>
<allow users="userName" />
<deny users="*" />
</authorization>
</system.web>
</configuration>
要考虑的另一件事是创建配置文件的读/写权限,但我认为您已经拥有它,因为动态文件夹创建。
希望这有帮助。