This topic has not yet been rated - Rate this topic

ServiceAuthorizationManager Class

Provides authorization access checking for service operations.

System.Object
  System.ServiceModel.ServiceAuthorizationManager

Namespace:  System.ServiceModel
Assembly:  System.ServiceModel (in System.ServiceModel.dll)
public class ServiceAuthorizationManager

The ServiceAuthorizationManager type exposes the following members.

  Name Description
Public method ServiceAuthorizationManager Initializes a new instance of the ServiceAuthorizationManager class.
Top
  Name Description
Public method CheckAccess(OperationContext) Checks authorization for the given operation context.
Public method CheckAccess(OperationContext, Message) Checks authorization for the given operation context when access to a message is required.
Protected method CheckAccessCore Checks authorization for the given operation context based on default policy evaluation.
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Protected method GetAuthorizationPolicies Gets the set of policies that participate in policy evaluation.
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Top

This class is responsible for evaluating all policies (rules that define what a user is allowed to do), comparing the policies to claims made by a client, setting the resulting AuthorizationContext to the ServiceSecurityContext, and providing the authorization decision whether to allow or deny access for a given service operation for a caller.

The CheckAccessCore method is called by the infrastructure each time an attempt to access a resource is made. The method returns true or false to allow or deny access, respectively.

The ServiceAuthorizationManager is part of the Identity Model infrastructure. The Identity Model enables you to create custom authorization policies and custom authorization schemes. For more information about how the Identity Model works, see Claims and Authorization.

Custom Authorization

This class does not perform any authorization and allows users to access all service operations. To provide more restrictive authorization, you must create a custom authorization manager that checks custom policies. To do this, inherit from this class and override the CheckAccessCore method. Specify the instance of the derived class through the ServiceAuthorizationManager property.

In CheckAccessCore, the application can use the OperationContext object to access the caller identity (ServiceSecurityContext).

By getting the IncomingMessageHeaders property, which returns a MessageHeaders object, the application can access the service (To), and the operation (Action).

By getting the RequestContext property, which returns a RequestContext object, the application can access the entire request message (RequestMessage) and perform the authorization decision accordingly.

For an example, see How To: Create a Custom AuthorizationManager for a Service.

To create custom authorization policies, implement the IAuthorizationPolicy class. For an example, see How To: Create a Custom Authorization Policy.

To create a custom claim, use the Claim class. For an example, see How To: Create a Custom Claim. To compare custom claims, you must compare claims, as shown in How To: Compare Claims.

For more information, seeCustom Authorization.

You can set the type of a custom authorization manager using the <serviceAuthorization> element in a client application configuration file.

The following example shows a class named MyServiceAuthorizationManager that inherits from the ServiceAuthorizationManager and overrides the CheckAccessCore method.


  public class MyServiceAuthorizationManager : ServiceAuthorizationManager
  {
	protected override bool CheckAccessCore(OperationContext operationContext)
	{                
	  // Extract the action URI from the OperationContext. Match this against the claims
	  // in the AuthorizationContext.
	  string action = operationContext.RequestContext.RequestMessage.Headers.Action;
	  
	  // Iterate through the various claim sets in the AuthorizationContext.
	  foreach(ClaimSet cs in operationContext.ServiceSecurityContext.AuthorizationContext.ClaimSets)
	  {
		// Examine only those claim sets issued by System.
		if (cs.Issuer == ClaimSet.System)
		{
		  // Iterate through claims of type "http://www.contoso.com/claims/allowedoperation".
            foreach (Claim c in cs.FindClaims("http://www.contoso.com/claims/allowedoperation", Rights.PossessProperty))
		  {
			// If the Claim resource matches the action URI then return true to allow access.
			if (action == c.Resource.ToString())
			  return true;
		  }
		}
	  }
	  
	  // If this point is reached, return false to deny access.
	  return false;                 
	}
  }


.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
How To Use ServiceAuthorizationManager to do APIKey Verification with WebHTTP (REST) Services
You can use ServiceAuthorizationManager to control access to WebHTTP (REST) Services by using an APIKey.  This is commonly done for public services on the web.

For more information - see http://blogs.msdn.com/b/rjacobs/archive/2010/06/14/how-to-do-api-key-verification-for-rest-services-in-net-4.aspx