Share via


ClaimsAuthorizationManager.LoadCustomConfiguration(XmlNodeList) 方法

定义

在派生类中重写时,将从 XML 中加载自定义配置。

public:
 virtual void LoadCustomConfiguration(System::Xml::XmlNodeList ^ nodelist);
public virtual void LoadCustomConfiguration (System.Xml.XmlNodeList nodelist);
abstract member LoadCustomConfiguration : System.Xml.XmlNodeList -> unit
override this.LoadCustomConfiguration : System.Xml.XmlNodeList -> unit
Public Overridable Sub LoadCustomConfiguration (nodelist As XmlNodeList)

参数

nodelist
XmlNodeList

自定义配置元素。 列表中的每个节点都属于 XmlElement 类型。

实现

示例

主题中使用的 ClaimsAuthorizationManager 代码示例取自示例 Claims Based Authorization 。 此示例提供了一个自定义声明授权管理器,该管理器可以根据配置中指定的策略授权使用者。 自定义声明授权管理器由三个基本组件组成:派生自 ClaimsAuthorizationManager 实现管理器的类、 ResourceAction 配对资源和操作的类,以及读取和编译配置文件中指定的策略的策略读取器。 然后,声明授权管理器可以使用此编译的策略来评估主体,以便授权访问资源。 为了简洁起见,并非所有元素都显示。 有关此示例和可用于 WIF 的其他示例以及下载位置的信息,请参阅 WIF 代码示例索引

以下代码显示了 方法的 LoadCustomConfiguration 重写。 此方法使用帮助程序策略读取器类, () 不显示来读取和编译配置文件中指定的授权策略。 策略被添加到字典中,并由一个 ResourceAction 键对象进行访问,该对象是从它们的目标资源和操作创建的。

static Dictionary<ResourceAction, Func<ClaimsPrincipal, bool>> _policies = new Dictionary<ResourceAction, Func<ClaimsPrincipal, bool>>();
PolicyReader _policyReader = new PolicyReader();
/// <summary>
/// Overloads  the base class method to load the custom policies from the config file
/// </summary>
/// <param name="nodelist">XmlNodeList containing the policy information read from the config file</param>
public override void LoadCustomConfiguration(XmlNodeList nodelist)
{
    Expression<Func<ClaimsPrincipal, bool>> policyExpression;

    foreach (XmlNode node in nodelist)
    {
        //
        // Initialize the policy cache
        //
        XmlDictionaryReader rdr = XmlDictionaryReader.CreateDictionaryReader(new XmlTextReader(new StringReader(node.OuterXml)));
        rdr.MoveToContent();

        string resource = rdr.GetAttribute("resource");
        string action = rdr.GetAttribute("action");

        policyExpression = _policyReader.ReadPolicy(rdr);

        //
        // Compile the policy expression into a function
        //
        Func<ClaimsPrincipal, bool> policy = policyExpression.Compile();

        //
        // Insert the policy function into the policy cache
        //
        _policies[new ResourceAction(resource, action)] = policy;
    }
}

以下代码显示了 ResourceAction 自定义声明管理器使用的 类。

using System;

namespace ClaimsAuthorizationLibrary
{
    /// <summary>
    /// Class to encapsulate resource/action pair
    /// </summary>
    public class ResourceAction
    {
        public string Resource;
        public string Action;

        /// <summary>
        /// Checks if the current instance is equal to the given object by comparing the resource and action values
        /// </summary>
        /// <param name="obj">object to compare to</param>
        /// <returns>True if equal, else false.</returns>
        public override bool Equals(object obj)
        {
            ResourceAction ra = obj as ResourceAction;
            if (ra != null)
            {
                return ((string.Compare(ra.Resource, Resource, true) == 0) && (string.Compare(ra.Action, Action, true) == 0));
            }

            return base.Equals(obj);
        }

        /// <summary>
        /// Gets the hash code.
        /// </summary>
        /// <returns>The hash code.</returns>
        public override int GetHashCode()
        {
            return (Resource + Action).ToLower().GetHashCode();
        }

        /// <summary>
        /// Creates an instance of ResourceAction class.
        /// </summary>
        /// <param name="resource">The resource name.</param>
        /// <param name="action">The action.</param>
        /// <exception cref="ArgumentNullException">when <paramref name="resource"/> is null</exception>
        public ResourceAction(string resource, string action)
        {
            if (string.IsNullOrEmpty(resource))
            {
                throw new ArgumentNullException("resource");
            }

            Resource = resource;
            Action = action;
        }
    }
}

声明授权管理器使用的策略是由 claimsAuthorizationManager> 元素下的<自定义<policy>元素指定的。 此策略由 LoadCustomConfiguration 方法读取和编译。 在第一个策略中,主体必须拥有指定的声明之一,才能对指定的资源执行指定操作。 第二个策略中,主体必须拥有这两个声明,才能对指定的资源执行指定操作。 在所有其他情况下,无论主体拥有何种声明,都会自动授予主体访问权限。

<system.identityModel>
  <identityConfiguration>
    <claimsAuthorizationManager type="ClaimsAuthorizationLibrary.MyClaimsAuthorizationManager, ClaimsAuthorizationLibrary">
      <policy resource="http://localhost:28491/Developers.aspx" action="GET">
        <or>
          <claim claimType="http://schemas.microsoft.com/ws/2008/06/identity/claims/role" claimValue="developer" />
          <claim claimType="http://schemas.xmlsoap.org/claims/Group" claimValue="Administrator" />
        </or>
      </policy>
      <policy resource="http://localhost:28491/Administrators.aspx" action="GET">
        <and>
          <claim claimType="http://schemas.xmlsoap.org/claims/Group" claimValue="Administrator" />
          <claim claimType="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/country" claimValue="USA" />
        </and>
      </policy>
      <policy resource="http://localhost:28491/Default.aspx" action="GET">
      </policy>
      <policy resource="http://localhost:28491/" action="GET">
      </policy>
      <policy resource="http://localhost:28491/Claims.aspx" action="GET">
      </policy>
    </claimsAuthorizationManager>

    ...

  </identityConfiguration>
</system.identityModel>

注解

方法 LoadCustomConfiguration 由配置基础结构调用。 调用此方法时, nodelist 将包含配置文件中 <claimsAuthorizationManager> 元素的顶级子元素。 这些元素中的每一个都可能包含属性或子元素,具体取决于为派生类定义的配置架构。 如果配置文件中的 元素下 <claimsAuthorizationManager> 没有显示子元素,则不会调用此方法。

默认实现引发 NotImplementedException。 在派生类中重写此方法,以便从配置文件初始化声明授权管理器。 通常,配置元素用于表示授权策略;但是,可以定义元素,并根据应用程序的要求以任何有意义的方式使用它们。

适用于