遗忘海岸

江湖程序员 -Feiph(LM战士)

导航

.net2.0 中自己定义配置的实现


   实现web.config中的自定义如下配置:
  <?xml version="1.0"?>
<!--
    注意: 除了手动编辑此文件以外,您还可以使用
    Web 管理工具来配置应用程序的设置。可以使用 Visual Studio 中的
     “网站”->“Asp.Net 配置”选项。
    设置和注释的完整列表在
    machine.config.comments 中,该文件通常位于
    \Windows\Microsoft.Net\Framework\v2.x\Config 中
-->
<configuration>
 <configSections>
  <sectionGroup name="gyzs.net" type="ComponentSectionGroup">
     <section name="security" type="SecuritySection"/>
  </sectionGroup>

 </configSections>
...............
<system.web>
..............
</system.web>
 <gyzs.net>
  <security configSource="config\security.config" />
 </gyzs.net>

</configuration>

config\security.config 文件内容如下
<?xml version="1.0" encoding="utf-8"?>
<!-- 注意: 除了手动编辑此文件以外,您还可以使用 Web 管理工具来
     配置应用程序的设置。可以使用 Visual Studio 中的“网站”->“ASP.NET 配置”
     选项。
     设置和注释的完整列表在
     machine.config.comments 中,该文件通常位于 \Windows\Microsoft.NET\Framework\v2.0.xxxxx\Config 中
 -->
<security enabled="true">
 <checkPermissionPages mode="Windows">
  <page url="AdminUser.aspx" operateCode="Del" checkType="or"/>
  <page url="AdminUser2.aspx" operateCode="Add" checkType="or"/>
  <page url="AdminUser3.aspx" operateCode="Modify" checkType="or"/>
 </checkPermissionPages>
</security>
实现代码

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Security 的摘要说明
/// </summary>


public class ComponentSectionGroup : ConfigurationSectionGroup
{
    [ConfigurationProperty("security")]
    public SecuritySection SecuritySection
    {
        get { return (SecuritySection)base.Sections["security"]; }
    }
}
/// <summary>
/// 安全配置节
/// </summary>
public class SecuritySection : ConfigurationSection
{
    private static readonly ConfigurationProperty _enabled = new ConfigurationProperty("enabled", typeof(bool), true, ConfigurationPropertyOptions.None);
    private static readonly ConfigurationProperty _checkPermissionPages = new ConfigurationProperty("checkPermissionPages", typeof(CheckPermissionPagesElement));
    private static ConfigurationPropertyCollection _properties = new ConfigurationPropertyCollection();

    static SecuritySection()
    {
        _properties.Add(_enabled);
        _properties.Add(_checkPermissionPages);
    }
    public bool Enabled
    {
        get { return (bool)base[_enabled]; }
    }
    public CheckPermissionPagesElement CheckPermissionPages
    {
        get { return (CheckPermissionPagesElement)base[_checkPermissionPages]; }
    }
    protected override ConfigurationPropertyCollection Properties
    {
        get
        {
            return _properties;
        }
    }
}
/// <summary>
/// 元素
/// </summary>
public class CheckPermissionPagesElement : ConfigurationElement
{
    private static readonly ConfigurationProperty _pages = new ConfigurationProperty(null, typeof(PageElementCollection), null, ConfigurationPropertyOptions.IsDefaultCollection);
    private static readonly ConfigurationProperty _mode = new ConfigurationProperty("mode", typeof(ModeOptions), ModeOptions.Form, ConfigurationPropertyOptions.None);
    private static ConfigurationPropertyCollection _properties = new ConfigurationPropertyCollection();

    static CheckPermissionPagesElement()
    {
        _properties.Add(_pages);
        _properties.Add(_mode);
    }
    public ModeOptions Mode
    {
        get { return (ModeOptions)this[_mode]; }
    }
    public PageElementCollection Pages
    {
        get { return (PageElementCollection)this[_pages]; }
    }
    protected override ConfigurationPropertyCollection Properties
    {
        get
        {
            return _properties;
        }
    }

}
public class PageElementCollection : ConfigurationElementCollection
{

    protected override ConfigurationElement CreateNewElement()
    {
        return new PageElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((PageElement)element).Url;
    }
    protected override string ElementName
    {
        get
        {
            return "page";
        }
    }
    public override ConfigurationElementCollectionType CollectionType
    {
        get
        {
            return ConfigurationElementCollectionType.BasicMap;
        }
    }
    public PageElement this[int index]
    {
        get { return (PageElement)base.BaseGet(index); }
    }
    public PageElement this[string url]
    {
        get { return (PageElement)base.BaseGet(url); }
    }
}
public class PageElement : ConfigurationElement
{
    private static readonly ConfigurationProperty _url = new ConfigurationProperty("url", typeof(string), string.Empty);
    private static readonly ConfigurationProperty _operateCode = new ConfigurationProperty("operateCode", typeof(string), string.Empty);
    private static readonly ConfigurationProperty _checkType = new ConfigurationProperty("checkType", typeof(string), "or");
    private static ConfigurationPropertyCollection _properties = new ConfigurationPropertyCollection();
    static PageElement()
    {
        _properties.Add(_url);
        _properties.Add(_operateCode);
        _properties.Add(_checkType);
    }

    public string Url
    {
        get { return (string)this[_url]; }
    }
    public string OperateCode
    {
        get { return (string)this[_operateCode]; }
    }
    public string CheckType
    {
        get { return (string)this[_checkType]; }
    }
    protected override ConfigurationPropertyCollection Properties
    {
        get
        {
            return _properties;
        }
    }
}

public enum ModeOptions { Form, Windows }

 ------------------
测试代码 page_load事件
        Configuration config = WebConfigurationManager.OpenWebConfiguration("/ajax/web.config");
        ComponentSectionGroup group = config.GetSectionGroup("gyzs.net") as ComponentSectionGroup;
        SecuritySection sec = group.SecuritySection;
        foreach (PageElement page in sec.CheckPermissionPages.Pages)
        {
                Response.Write(page.Url + "<br>");
        }
        Response.Write(sec.CheckPermissionPages.Mode.ToString());

代码下载

posted on 2008-02-20 12:20  遗忘海岸  阅读(421)  评论(0编辑  收藏  举报