开发者

How can I add location elements programmatically to the web config?

开发者 https://www.devze.com 2023-04-11 19:34 出处:网络
I have an application which creates page routes from a database. My whole site is secured with forms authentication but I need to allow unauthenticated uses to access these routes. I don\'t want to ha

I have an application which creates page routes from a database. My whole site is secured with forms authentication but I need to allow unauthenticated uses to access these routes. I don't want to hard-code <location> tags for the routes in the web.config as th开发者_JAVA技巧is will negate me using a database to generate the routes.

Can anyone help?


Thanks everyone. I've found an answer here

Basically it involves creating a folder for each route and putting a web.config file in it allowing access. This approach needs to be coupled with setting RouteExistingFiles to false so that the routes don't get confused with the folders.


Rather than using strongly typed configuration classes, why not make the modifications directly in XML?

Here's an abbreviated snippet to demonstrate the concept from some code of mine that performance IIS tuning in the machine.config. The principal is the same for other XML config files though. You just need to create the appropriate XPath statements to do what you need.

XmlDocument machineConfigFile = new XmlDocument();
machineConfigFile.Load(MachineConfigPathString);

XmlNode autoConfig = machineConfigFile.SelectSingleNode(@"/configuration/system.web/processModel/@autoConfig");
autoConfig.Value = "false";

machineConfigFile.Save(MachineConfigPathString);

When saved, the XmlDocument object will preserve all other untouched document nodes. Very handy. It works great for modifying the machine.config. The only possible issue I can see is that your application will probably reset when you save your changes to the web.config. So test it out in a safe environment with a backup of your web.config just in case the reset causes any undesired outcomes!


I found this MSDN link for you. I didn't find whether you can modify the config of running server instance this way though.


Have you considered implimenting your site security in a different way? Having a portion of the site that allows unauthenticated access and a portion that does not. I am "assuming" (bad) that you are using MVC since you are describing routes - this is very easy to do with both MVC and traditional web form applications.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号