SecurityTokenHandler.CreateToken(SecurityTokenDescriptor) Method

Definition

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

public:
 virtual System::IdentityModel::Tokens::SecurityToken ^ CreateToken(System::IdentityModel::Tokens::SecurityTokenDescriptor ^ tokenDescriptor);
public virtual System.IdentityModel.Tokens.SecurityToken CreateToken (System.IdentityModel.Tokens.SecurityTokenDescriptor tokenDescriptor);
abstract member CreateToken : System.IdentityModel.Tokens.SecurityTokenDescriptor -> System.IdentityModel.Tokens.SecurityToken
override this.CreateToken : System.IdentityModel.Tokens.SecurityTokenDescriptor -> System.IdentityModel.Tokens.SecurityToken
Public Overridable Function CreateToken (tokenDescriptor As SecurityTokenDescriptor) As SecurityToken

Parameters

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

Returns

A security token that matches the properties of the token descriptor.

Examples

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;
}

Remarks

By default this method throws a NotImplementedException exception.

Called from implementations of the SecurityTokenService class.

Applies to