ClaimsAuthorizationManager.CheckAccess(AuthorizationContext) メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
派生クラスで実装されている場合は、指定したリソースに対して指定したアクションを実行するために、指定したコンテキスト内のサブジェクトの承認を確認します。
public:
virtual bool CheckAccess(System::Security::Claims::AuthorizationContext ^ context);
public virtual bool CheckAccess(System.Security.Claims.AuthorizationContext context);
abstract member CheckAccess : System.Security.Claims.AuthorizationContext -> bool
override this.CheckAccess : System.Security.Claims.AuthorizationContext -> bool
Public Overridable Function CheckAccess (context As AuthorizationContext) As Boolean
パラメーター
- context
- AuthorizationContext
承認を確認する対象、リソース、およびアクションを含む承認コンテキスト。
返品
true サブジェクトが指定されたリソースに対して指定されたアクションを実行する権限を持つ場合。それ以外の場合は false。
例
ClaimsAuthorizationManagerトピックで使用されるコード例は、Claims Based Authorization サンプルから取得します。 このサンプルでは、構成で指定されているポリシーに基づいてサブジェクトを承認できるカスタム要求承認マネージャーを提供します。 カスタム要求承認マネージャーは、3 つの基本的なコンポーネントで構成されます。マネージャーを実装する ClaimsAuthorizationManager から派生したクラス、リソースとアクションを組み合わせた ResourceAction クラス、構成ファイルで指定されたポリシーを読み取ってコンパイルするポリシー リーダーです。 このコンパイル済みポリシーは、要求承認マネージャーがリソースへのアクセスを承認するためにプリンシパルを評価するために使用できます。 簡潔にするためにすべての要素が表示されるわけではありません。
次のコードは、 CheckAccess メソッドのオーバーライドを示しています。 このメソッドは、構成ファイルから読み取られ、コンパイルされたポリシーに基づいてアクセスを許可または拒否します。
static Dictionary<ResourceAction, Func<ClaimsPrincipal, bool>> _policies = new Dictionary<ResourceAction, Func<ClaimsPrincipal, bool>>();
PolicyReader _policyReader = new PolicyReader();
/// <summary>
/// Checks if the principal specified in the authorization context is authorized to perform action specified in the authorization context
/// on the specified resoure
/// </summary>
/// <param name="pec">Authorization context</param>
/// <returns>true if authorized, false otherwise</returns>
public override bool CheckAccess(AuthorizationContext pec)
{
//
// Evaluate the policy against the claims of the
// principal to determine access
//
bool access = false;
try
{
ResourceAction ra = new ResourceAction(pec.Resource.First<Claim>().Value, pec.Action.First<Claim>().Value);
access = _policies[ra](pec.Principal);
}
catch (Exception)
{
access = false;
}
return access;
}
}
次のコードは、カスタム クレーム マネージャーによって使用される 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;
}
}
}
要求承認マネージャーによって使用されるポリシーは、<policy>> 要素のカスタム 要素によって指定されます。 このポリシーは、 LoadCustomConfiguration メソッドによって読み取られ、コンパイルされます。 最初のポリシーでは、指定したリソースに対して指定したアクションを実行するには、プリンシパルが指定された要求のいずれかを所有している必要があります。 2 番目のポリシーでは、指定したリソースに対して指定したアクションを実行できるようにするには、プリンシパルが両方の要求を保持している必要があります。 それ以外の場合、プリンシパルには、所有している要求に関係なく、自動的にアクセス権が付与されます。
<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>
注釈
基本実装では常に trueが返され、アクセスが承認されます。 派生クラスでこのメソッドをオーバーライドして、RP アプリケーションの要件に基づいてアクセスを承認できます。 このメソッドが false を返す場合、Windows Identity Foundation (WIF) は呼び出し元に未承認のエラーを返します。それ以外の場合は、RP アプリケーションに実行が渡されます。