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。 在你的衍生類別中覆寫此方法,以啟用從組態檔案初始化你的理賠授權管理器。 通常,配置元素用來表達授權政策;不過,你可以根據應用程式的需求定義元素並以任何合理的方式使用它們。