This documentation is archived and is not being maintained.

ServiceSecurityContext Class

Represents the security context of a remote party. On the client, represents the service identity and, on the service, represents the client identity.


Namespace:  System.ServiceModel
Assembly:  System.ServiceModel (in System.ServiceModel.dll)

public class ServiceSecurityContext

The ServiceSecurityContext type exposes the following members.

Public methodServiceSecurityContext(AuthorizationContext)Initializes a new instance of the ServiceSecurityContext class with the specified authorization parameters.
Public methodServiceSecurityContext(ReadOnlyCollection<IAuthorizationPolicy>)Initializes a new instance of the ServiceSecurityContext class with the collection of policies object.
Public methodServiceSecurityContext(AuthorizationContext, ReadOnlyCollection<IAuthorizationPolicy>)Initializes a new instance of the ServiceSecurityContext class with the specified authorization parameters and collection of policies.

Public propertyStatic memberAnonymousReturns an instance of the ServiceSecurityContext class that contains an empty collection of claims, identities, and other context data that is usually used to represent an anonymous party.
Public propertyAuthorizationContextGets the authorization information for an instance of this class. The AuthorizationContext contains a collection of ClaimSet that the application can interrogate and retrieve the information of the party.
Public propertyAuthorizationPoliciesGets the collection of policies associated with an instance of this class.
Public propertyStatic memberCurrentGets the current ServiceSecurityContext.
Public propertyIsAnonymousGets a value that indicates whether the current client has provided credentials to the service.
Public propertyPrimaryIdentityGets the primary identity associated with the current setting.
Public propertyWindowsIdentityGets the Windows identity of the current setting.

Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)

The data is part of the SecurityMessageProperty for a message.

Use this class to obtain information about a remote security context at runtime. A security context is created when a client is successfully authenticated and authorized to access a method. When a message is successfully authenticated and authorized, the security information from the client and for the current service instance can be obtained from an instance of this class.

You can retrieve an instance of the ServiceSecurityContext from the Current property of the OperationContext class, or use it from within a service operation method, as shown in the following example.

Parsing a ClaimSet

A common use of the class is to retrieve the current set of claims for the purpose of identifying or authorizing a client when accessing a method. The ClaimSet class contains a collection of Claim objects, and each can be parsed to determine whether a specific claim is present. If the specified claim is provided, authorization can be granted. This functionality is provided by overriding the CheckAccessCore method of the ServiceAuthorizationManager class. For a complete example, see the Authorization Policy Sample.

Cookie Mode and IsAuthenticated

Note that under some circumstances, the IsAuthenticated property of the IIdentity interface returns true even if the remote client is authenticated as an anonymous user. (The PrimaryIdentity property returns an implementation of the IIdentity interface.) The following circumstances must be true for this to occur:

  • The service uses Windows authentication.

  • The service allows anonymous logons.

  • The binding is a customBinding Element.

  • The custom binding includes a <security> element.

  • The <security> element includes a secureConversationBootstrap element with the requireSecurityContextCancellation attribute set to false.

The following example uses the ServiceSecurityContext class to provide information about the current security context. The code creates an instance of the StreamWriter class to write the information to a file.

// When this method runs, the caller must be an authenticated user 
// and the ServiceSecurityContext is not a null instance. 
public double Add(double n1, double n2)
    // Write data from the ServiceSecurityContext to a file using the StreamWriter class.
    using (StreamWriter sw = new StreamWriter(@"c:\ServiceSecurityContextInfo.txt"))
        // Write the primary identity and Windows identity. The primary identity is derived from 
        // the credentials used to authenticate the user. The Windows identity may be a null string.
        sw.WriteLine("PrimaryIdentity: {0}", ServiceSecurityContext.Current.PrimaryIdentity.Name);
        sw.WriteLine("WindowsIdentity: {0}", ServiceSecurityContext.Current.WindowsIdentity.Name);

        // Write the claimsets in the authorization context. By default, there is only one claimset
        // provided by the system. 
        foreach (ClaimSet claimset in ServiceSecurityContext.Current.AuthorizationContext.ClaimSets)
            foreach (Claim claim in claimset)
                // Write out each claim type, claim value, and the right. There are two
                // possible values for the right: "identity" and "possessproperty". 
                sw.WriteLine("Claim Type: {0}, Resource: {1} Right: {2}",
    return n1 + n2;

The following example shows an implementation of the CheckAccessCore method that uses the ServiceSecurityContext to parse a set of claims.

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;
        Console.WriteLine("action: {0}", action);

        // Iterate through the various claimsets 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 "".
                foreach (Claim c in cs.FindClaims("", 
                    // Write the Claim resource to the console.
                    Console.WriteLine("resource: {0}", c.Resource.ToString());

                    // 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.