自定义配置文件C#以根据用户的角色重定向用户

时间:2015-01-21 19:50:28

标签: c# asp.net asp.net-mvc asp.net-mvc-4 web-config

我正在尝试编写正确的配置文件,但我认为我混淆了几个定义。最终目的是根据用户的角色将用户重定向到正确的页面。

问题:

  • 具有多个标签的嵌套元素不是a ConfigurationElementCollection中?
  • 单个元素不是 配置元素?

如果这是正确的,这是我在下面的代码上的错误?

这是我的例子,在我的webconfig上我写了这个:

<configSections>
        <section name="adminSecurityConfig" type="Web.Portal.Authentication.ViewModels.RoleConfiguration.AdminSecuritySection" />
</configSections>

<!--Roles for Web Portals and a link to an external file-->
<adminSecurityConfig configSource="AdminSecurity.xml" />

我的AdminSecurity.xml文件如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<adminSecurityConfig>
  <role name="Employee">
    <redirectUrl src="www.google.com"></redirectUrl>
    <redirectUrl src="www.yahoo.com"></redirectUrl>
    <redirectUrl src="www.amazon.com"></redirectUrl>
  </role>
  <role name="Member">
    <redirectUrl src="www.yahoo.com"></redirectUrl>
  </role>
  <role name="Group Administrator">
  </role>
</adminSecurityConfig>

我有一个帮手使用ConfigSection

public class ConfigurationRoleManager
{
    public const string seccionName = "adminSecurityConfig";

    public static AdminSecuritySection GetConfig()
    {
        return (AdminSecuritySection)ConfigurationManager.GetSection(seccionName);
    }
}

此外,我还有几个类可以访问xml中的元素。我正在使用 - &gt;表示包含在下面的字符串

AdminSecuritySection - &gt; RoleCollection - &gt; RoleElement(具有Role的name属性) - &gt; UrlElementCollection - &gt; UrlElement(带有重定向源)

以下是课程:

  • AdminSecuritySection

    public class AdminSecuritySection : ConfigurationSection
    {
        [ConfigurationProperty("role", IsDefaultCollection = false)]
        ConfigurationCollection(typeof(RoleCollection))]
        public RoleCollection Roles
        {
            get
            {
                return this["role"] as RoleCollection ?? new RoleCollection();
            }
            set { this["role"] = value; }
        }
    }
    
  • RoleCollection

    public class RoleCollection : ConfigurationElementCollection
    {
        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.BasicMap;
            }
        }
    
        protected override ConfigurationElement CreateNewElement()
        {
            return new RoleElement();
        }
    
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((RoleElement)element).Name;
        }
    
        protected override string ElementName
        {
            get { return "role"; }
        }
    
        public void Add(RoleElement element)
        {
            base.BaseAdd(element);
        }
    
        public void Remove(RoleElement element)
        {
            base.BaseRemove(element);
        }
    
        public RoleElement this[string name]
        {
            get
            {
                foreach (var item in this)
                {
                    if ((item as RoleElement).Name == name)
                        return item as RoleElement;
                }
    
                return null;
            }
        }
    }
    
  • RoleElement

    public class RoleElement : ConfigurationElement
    {
        [ConfigurationProperty("redirectUrl", IsDefaultCollection = false)]
        [ConfigurationCollection(typeof(UrlElementCollection))]
        public UrlElementCollection Urls
        {
            get { return this["redirectUrl"] as UrlElementCollection ?? new UrlElementCollection(); }
            set { this["redirectUrl"] = value; }
        }
    
        [ConfigurationProperty("name", IsRequired = true)]
        public string Name
        {
            get { return (string)this["name"]; }
            set { this["name"] = value; }
        }
    }
    
  • UrlElementCollection

    public class UrlElementCollection : ConfigurationElementCollection
    {
        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.BasicMap;
            }
        }
    
        protected override string ElementName
        {
            get
            {
                return "redirectUrl";
            }
        }
    
        protected override ConfigurationElement CreateNewElement()
        {
            return new UrlElement();
        }
    
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((UrlElement)element).Source;
        }
    
        public void Add(UrlElement element)
        {
            this.BaseAdd(element);
        }
    
        public void Remove(UrlElement element)
        {
            this.BaseRemove(element);
        }
    
        public UrlElement this[string source]
        {
            get
            {
                foreach (var item in this)
                {
                    if ((item as UrlElement).Source == source)
                        return item as UrlElement;
                }
    
                return null;
            }
        }
    }
    
  • UrlElement

    public class UrlElement : ConfigurationElement
    {
        [ConfigurationProperty("src", IsRequired = true)]
        public string Source
        {
            get { return (string)this["src"]; }
            set { this["src"] = value; }
        }
    }
    

当我尝试运行它时,会出现此错误:

enter image description here

1 个答案:

答案 0 :(得分:1)

看起来您的RoleCollection和RoleElement都具有相同的名称,即角色。这可能令人困惑。当遇到角色标记时,它将被视为没有name属性的角色集合。将RoleCollection名称更改为角色和xml结构,如下所示。

<?xml version="1.0" encoding="utf-8" ?>
  <adminSecurityConfig>
    <roles>
     <role name="Employee">
        <redirectUrl src="www.google.com"></redirectUrl>
        <redirectUrl src="www.yahoo.com"></redirectUrl>
        <redirectUrl src="www.amazon.com"></redirectUrl>
     </role>
     <role name="Member">
        <redirectUrl src="www.yahoo.com"></redirectUrl>
     </role>
     <role name="Group Administrator">
     </role>
   </roles>
  </adminSecurityConfig>