开发者

How can I get a list of pages that allow all users from web.config? [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

How get web config location element?

My web.config has a few pages that we alllow unauthenticated users to view. Is there a way to loop through this list at runtime?

  <location path="default.aspx">
    <system.web>
      <authorization>
        <allow users="*"/>
      </authorization>
    </system.web>
  </location>
  <location path="default2.aspx">
    <system.web>
      <authorization>
        <allow users="*"/>
      </authorization>
    </system.web>
  <开发者_开发知识库;/location>

EDIT

I want a list of all unauthorized pages so that I can exlude them from some javascript code that pops up a reminder when their session is about to time out.

http://www.erichynds.com/examples/jquery-idle-timeout/example-mint.htm


Look into SiteMaps, which can be trimmed by authorization and iterated via loops like SiteMap.CurrentNode.ChildNodes. You would also need to maintain a SiteMap.xml with a complete sitemap matching your page structure for this to work.

If, for example, your goal is generate a site navigation menu specifically for non-authorized users, then there are built-in controls like SiteMapDataSource which when combined with Menu control can generate menus tailored to the user's current role authorization automatically, for authorized and non-authorized users alike.


You can use this: System.Configuration.ConfigurationLocation

There is sample code that shows getting the ConfigurationLocationCollection. You should then be able to iterate over that collection, get the Path property and do what you are looking to do.

EDIT: I was able to properly read the web.config using this code:

ExeConfigurationFileMap configFileMap =
new ExeConfigurationFileMap();
configFileMap.ExeConfigFilename = Server.MapPath("/web.config");
Configuration config =
    ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
ConfigurationLocationCollection myLocationCollection = config.Locations;
foreach (ConfigurationLocation myLocation in myLocationCollection) {
    Response.Write(String.Format("Location Path: {0}", myLocation.Path));
    Configuration myLocationConfiguration = myLocation.OpenConfiguration();
    Response.Write(String.Format("Location Configuration File Path: {0}",
        myLocationConfiguration.FilePath));
}

However, just as you mentioned in the comments, the ConfigurationLocationCollection is empty! I have 6 location entries in my web.config, so this is definitely strange.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜