Export (0) Print
Expand All

SecurityTokenHandler.CreateToken Method

.NET Framework 4.5

When overridden in a derived class, creates a security token using the specified token descriptor. This method is called by a security token service (STS).

Namespace:  System.IdentityModel.Tokens
Assembly:  System.IdentityModel (in System.IdentityModel.dll)

public virtual SecurityToken CreateToken(
	SecurityTokenDescriptor tokenDescriptor


Type: System.IdentityModel.Tokens.SecurityTokenDescriptor

The security token descriptor from which the token is to be created. Properties of the token descriptor are set before this method is called.

Return Value

Type: System.IdentityModel.Tokens.SecurityToken
A security token that matches the properties of the token descriptor.

By default this method throws a NotImplementedException exception.

Called from implementations of the SecurityTokenService class.

The following code shows how to override the CreateToken method to create and return a token from a token descriptor. The code is taken from the Custom Token sample. This sample provides custom classes that enable processing of Simple Web Tokens (SWT). For information about this sample and other samples available for WIF and where to download them, see WIF Code Sample Index.

public override SecurityToken CreateToken(SecurityTokenDescriptor tokenDescriptor)
    if (tokenDescriptor == null)
        throw new ArgumentNullException("tokenDescriptor");

    NameValueCollection properties = new NameValueCollection();
    properties.Add(SimpleWebTokenConstants.Id, Guid.NewGuid().ToString());
    properties.Add(SimpleWebTokenConstants.Issuer, tokenDescriptor.TokenIssuerName);
    properties.Add(SimpleWebTokenConstants.Audience, tokenDescriptor.AppliesToAddress);
    properties.Add(SimpleWebTokenConstants.ExpiresOn, SecondsFromSwtBaseTime(tokenDescriptor.Lifetime.Expires));
    properties.Add(SimpleWebTokenConstants.ValidFrom, SecondsFromSwtBaseTime(tokenDescriptor.Lifetime.Created));

    foreach (Claim claim in tokenDescriptor.Subject.Claims)
        properties.Add(claim.Type, claim.Value);

    SimpleWebToken token = new SimpleWebToken(properties);
    return token;

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5

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.

© 2014 Microsoft