Export (0) Print
Expand All

ServiceCredentialsSecurityTokenManager.CreateSecurityTokenProvider Method

Creates a security token provider based on the SecurityTokenRequirement.

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

public override SecurityTokenProvider CreateSecurityTokenProvider(
	SecurityTokenRequirement requirement
)

Parameters

requirement
Type: System.IdentityModel.Selectors.SecurityTokenRequirement

The security token requirement.

Return Value

Type: System.IdentityModel.Selectors.SecurityTokenProvider
The security token provider.

ExceptionCondition
ArgumentNullException

requirement is null.

NotSupportedException

A security token provider cannot be created for the requirement that was passed in.

The SecurityTokenProvider class is responsible for obtaining tokens for a given ServiceModelSecurityTokenRequirement. Optionally, token providers can support renewing and canceling tokens as well. The token provider can cache tokens if it is configured to do so.

The following code shows how to override this method.

internal class MyServiceCredentialsSecurityTokenManager : 
    ServiceCredentialsSecurityTokenManager
{
    MyServiceCredentials credentials;

    public MyServiceCredentialsSecurityTokenManager(
        MyServiceCredentials credentials)
        : base(credentials)
    {
        this.credentials = credentials;
    }

    public override SecurityTokenProvider CreateSecurityTokenProvider(
        SecurityTokenRequirement requirement)
    {
        SecurityTokenProvider result = null;
        if (requirement.TokenType == SecurityTokenTypes.X509Certificate)
        {
            MessageDirection direction = requirement.
                GetProperty<MessageDirection>(
                ServiceModelSecurityTokenRequirement.
                MessageDirectionProperty);
            if (direction == MessageDirection.Input)
            {
                if (requirement.KeyUsage == SecurityKeyUsage.Exchange)
                {
                    result = new X509SecurityTokenProvider(
                        credentials.ServiceEncryptingCertificate);
                }
                else
                {
                    result = new X509SecurityTokenProvider(
                        credentials.ClientSigningCertificate);
                }
            }
            else
            {
                if (requirement.KeyUsage == SecurityKeyUsage.Signature)
                {
                    result = new X509SecurityTokenProvider(
                        credentials.ServiceSigningCertificate);
                }
                else
                {
                    result = new X509SecurityTokenProvider(
                        credentials.ClientEncryptingCertificate);
                }
            }
        }
        else
        {
            result = base.CreateSecurityTokenProvider(requirement);
        }
        return result;
    }
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft