ClaimsAuthorizationManager Class
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Defines the base implementation for a claims authorization manager.
public ref class ClaimsAuthorizationManager : System::IdentityModel::Configuration::ICustomIdentityConfiguration
public class ClaimsAuthorizationManager : System.IdentityModel.Configuration.ICustomIdentityConfiguration
type ClaimsAuthorizationManager = class
interface ICustomIdentityConfiguration
Public Class ClaimsAuthorizationManager
Implements ICustomIdentityConfiguration
- Inheritance
-
ClaimsAuthorizationManager
- Implements
Examples
The code examples that are used in the ClaimsAuthorizationManager topics are taken from the Claims Based Authorization
sample. This sample provides a custom claims authorization manager that can authorize subjects based on a policy that is specified in configuration. The custom claims authorization manager consists of three basic components: a class derived from ClaimsAuthorizationManager that implements the manager, the ResourceAction
class that pairs a resource and an action, and a policy reader that reads and compiles policy that is specified in the configuration file. This compiled policy can then be used by the claims authorization manager to evaluate a principal in order to authorize access to resources. Not all elements are shown for the sake of brevity. For information about this sample and other samples available for WIF and about where to download them, see WIF Code Sample Index.
The following code shows the implementation of the custom claims authorization manager. The LoadCustomConfiguration method reads and compiles the policy from configuration by using the policy reader helper class (not shown) and the CheckAccess method grants or denies access based on this policy.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Security.Claims;
using System.Xml;
namespace ClaimsAuthorizationLibrary
{
/// <summary>
/// Simple ClaimsAuthorizationManager implementation that reads policy information from the .config file
/// </summary>
public class MyClaimsAuthorizationManager : ClaimsAuthorizationManager
{
static Dictionary<ResourceAction, Func<ClaimsPrincipal, bool>> _policies = new Dictionary<ResourceAction, Func<ClaimsPrincipal, bool>>();
PolicyReader _policyReader = new PolicyReader();
/// <summary>
/// Creates a new instance of the MyClaimsAuthorizationManager
/// </summary>
public MyClaimsAuthorizationManager()
{
}
/// <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;
}
}
/// <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;
}
}
}
The following code shows the ResourceAction
class used by the custom claims manager.
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;
}
}
}
The following XML shows how to configure the claims authorization manager shown above for a web application hosted in IIS 7.5. Only the elements specific to the configuration of the claims authorization manager are shown. Note that a reference to the ClaimsAuthorizationModule class must be added to the pipeline under the <system.Webserver>
element. For sites and applications hosted in versions of IIS prior to IIS 7 the modules can be added to the pipeline under the <system.Web>
element. This configuration is shown but commented out.
The policy used by the claims authorization manager is specified by custom <policy>
elements under the <claimsAuthorizationManager> element. In the first policy, the principal must possess one of the specified claims in order to perform the specified action on the specified resource. In the second policy, the principal must possess both claims to be able to perform the specified action on the specified resource. In all others, the principal is automatically granted access regardless of the claims it possesses.
<configuration>
<configSections>
<!--WIF 4.5 sections -->
<section name="system.identityModel" type="System.IdentityModel.Configuration.SystemIdentityModelSection, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
...
</configSections>
...
<system.web>
<httpModules>
<!--WIF 4.5 modules -->
<!--Not needed here for IIS >= 7 -->
<!--<add name="ClaimsAuthorizationModule" type="System.IdentityModel.Services.ClaimsAuthorizationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>-->
</httpModules>
</system.web>
...
<system.webServer>
<modules>
<!--WIF 4.5 modules -->
<add name="ClaimsAuthorizationModule" type="System.IdentityModel.Services.ClaimsAuthorizationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</modules>
</system.webServer>
...
<!-- WIF 4.5 s.im section-->
<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>
...
</configuration><configuration>
<configSections>
<!--WIF 4.5 sections -->
<section name="system.identityModel" type="System.IdentityModel.Configuration.SystemIdentityModelSection, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
...
</configSections>
...
<system.web>
<httpModules>
<!--WIF 4.5 modules -->
<!--Not needed here for IIS >= 7 -->
<!--<add name="ClaimsAuthorizationModule" type="System.IdentityModel.Services.ClaimsAuthorizationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>-->
</httpModules>
</system.web>
...
<system.webServer>
<modules>
<!--WIF 4.5 modules -->
<add name="ClaimsAuthorizationModule" type="System.IdentityModel.Services.ClaimsAuthorizationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</modules>
</system.webServer>
...
<!-- WIF 4.5 s.im section-->
<system.identityModel>
<identityConfiguration>
<claimsAuthorizationManager type="MyClaimsAuthorizationManager.SimpleClaimsAuthorizationManager, MyClaimsAuthorizationManager" />
...
</system.identityModel>
...
</configuration>
Remarks
The ClaimsAuthorizationManager class provides the base implementation for a claims authorization manager. A claims authorization manager can be used in the following two scenarios:
In web-based applications and services, a claims authorization manager can be added to the processing pipeline to provide an extensibility point from which you can authorize access to a resource according to the value of incoming claims before the application code that actually implements the requested resource is called.
When you use the ClaimsPrincipalPermission class or the ClaimsPrincipalPermissionAttribute class to perform either imperative or declarative claims-based access checks in your code, the claims authorization manager that is configured for your application is called by the system to perform the check. Claims-based access checks can be performed in both web-based applications and desktop applications.
The default implementation provided by the ClaimsAuthorizationManager class authorizes access for each claim presented; however, you can derive from this class and override the CheckAccess method to provide your own authorization logic.
The use of a claims authorization manager is optional. You can configure your application to use a claims authorization manager either programmatically by using the IdentityConfiguration class or declaratively, by specifying the <claimsAuthorizationManager> element, which is a child element of the <identityConfiguration> element in your application configuration file. If your application is a web site or a web application hosted in Internet Information Services (IIS), you must also add the ClaimsAuthorizationModule in the ASP.NET HTTP Modules collection.
Important
When you use the ClaimsPrincipalPermission class or the ClaimsPrincipalPermissionAttribute class, the claims authorization manager that is used to perform the access check is the one that is specified in the identity configuration under the FederatedAuthentication.FederationConfiguration property. In a configuration file, it is the <identityConfiguration>
section that is referenced from the default <federationConfiguration>
element. This is true even for Windows Communication Foundation (WCF) services and desktop applications.
The ClaimsAuthorizationManager base class does not take any additional configuration; however, you can override the LoadCustomConfiguration in derived classes to provide initialization of your claims authorization manager from child elements of the <claimsAuthorizationElement>
. The typical scenario is to use these child elements to specify authorization policies which determine which claim types and values are required in order to gain access to which resource. This is not a hard requirement, though you are free to define whatever usage and syntax make sense for your implementation.
Constructors
ClaimsAuthorizationManager() |
Initializes a new instance of the ClaimsAuthorizationManager class. |
Methods
CheckAccess(AuthorizationContext) |
When implemented in a derived class, checks authorization for the subject in the specified context to perform the specified action on the specified resource. |
Equals(Object) |
Determines whether the specified object is equal to the current object. (Inherited from Object) |
GetHashCode() |
Serves as the default hash function. (Inherited from Object) |
GetType() |
Gets the Type of the current instance. (Inherited from Object) |
LoadCustomConfiguration(XmlNodeList) |
When overridden in a derived class, loads custom configuration from XML. |
MemberwiseClone() |
Creates a shallow copy of the current Object. (Inherited from Object) |
ToString() |
Returns a string that represents the current object. (Inherited from Object) |