This topic has not yet been rated - Rate this topic

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.
Exception Condition
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, 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.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ