如何从 web.config 获取允许所有用户的页面列表?

发布于 2024-11-06 19:57:08 字数 943 浏览 0 评论 0原文

可能的重复:
如何获取Web配置位置元素?

我的 web.config 有几个页面我们允许未经身份验证的用户查看。有没有办法在运行时循环遍历这个列表?

  <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>

编辑

我想要一个所有未经授权页面的列表,以便我可以将它们从一些 JavaScript 代码中排除,这些代码会在会话即将超时时弹出提醒。

http://www.erichynds.com/examples/jquery-空闲超时/example-mint.htm

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戒ㄋ 2024-11-13 19:57:08

查看 SiteMaps,它可以通过授权进行修剪并通过循环进行迭代,例如SiteMap.CurrentNode.ChildNodes。您还需要维护一个 SiteMap.xml ,其中包含与您的页面结构相匹配的完整站点地图,才能使其正常工作。

例如,如果您的目标是专门为非授权用户生成站点导航菜单,则可以使用诸如 SiteMapDataSource 之类的内置控件,将其与 Menu 控件结合使用时可以自动生成适合用户当前角色授权的菜单,适用于授权用户和非授权用户。

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.

|煩躁 2024-11-13 19:57:08

您可以使用: System.Configuration.ConfigurationLocation

那里是显示获取 ConfigurationLocationCollection 的示例代码。然后,您应该能够迭代该集合,获取 Path 属性并执行您想要执行的操作。

编辑:我能够使用此代码正确读取 web.config:

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));
}

但是,正如您在评论中提到的那样,ConfigurationLocationCollection 是空的!我的 web.config 中有 6 个位置条目,所以这绝对很奇怪。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文