CanReadToken Method (XmlReader)
Collapse the table of content
Expand the table of content

SecurityTokenHandler.CanReadToken Method (XmlReader)

.NET Framework 4.6 and 4.5

Returns a value that indicates whether the XML element referenced by the specified XML reader can be read as a token of the type processed by this instance.

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

public virtual bool CanReadToken(
	XmlReader reader


Type: System.Xml.XmlReader

An XML reader positioned at a start element. The reader should not be advanced by this method.

Return Value

Type: System.Boolean

true if the SecurityTokenHandler.ReadToken method can read the element; otherwise, false. The default is false.

The default implementation always returns false.

A derived class checks the element that the reader is referring to in order to determine whether the instance can deserialize a security token. This is typically accomplished through a call to the IsStartElement method with the appropriate element and namespace strings specified. If you override CanReadKeyIdentifierClause, you must also override the SecurityTokenHandler.ReadToken method or the SecurityTokenHandler.ReadToken method to provide the logic to deserialize the key identifier clause.

The following code shows how to override the CanReadToken method to determine whether a token can be read by a handler. 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.

/// <summary>
/// Indicates whether the current XML element can be read as a token of the type handled by this instance.
/// </summary>
/// <param name="reader">An XML reader positioned at a start element. The reader should not be advanced.</param>
/// <returns>True if the ReadToken method can the element.</returns>
public override bool CanReadToken( XmlReader reader )
    bool canRead = false;

    if ( reader != null )
        if ( reader.IsStartElement( BinarySecurityToken)
            && ( reader.GetAttribute( ValueType ) == SimpleWebTokenConstants.ValueTypeUri ) )
            canRead = true;

    return canRead;

.NET Framework
Available since 4.5
Return to top
© 2015 Microsoft