CreateToken Method

SecurityTokenHandler.CreateToken Method (SecurityTokenDescriptor)

.NET Framework (current version)
 

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
)

Parameters

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
Available since 4.5
Return to top
Show:
© 2016 Microsoft