SecurityTokenHandler.CanReadToken Method (XmlReader)

.NET Framework 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

Supported in: 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.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2014 Microsoft