ClaimsAuthorizationManager.LoadCustomConfiguration(XmlNodeList) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在派生类中重写时,从 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 配对资源和操作的类,以及读取和编译配置文件中指定的策略的策略读取器。 然后,声明授权管理器可以使用此编译的策略来评估主体,以便授权访问资源。 并非所有元素都是为了简洁起见而显示的。
以下代码显示了方法的 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;
}
}
}
声明授权管理器使用的策略由 >自定义元素指定。 此方法读取和编译 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。 在派生类中重写此方法,以便从配置文件中初始化声明授权管理器。 通常,配置元素用于表示授权策略;但是,可以定义元素,并根据应用程序的要求以任何有意义的方式使用它们。