This topic has not yet been rated - Rate this topic

WSSecurityTokenSerializer Class

The class used for serializing and deserializing security tokens, security key identifiers, and security key identifier clauses defined in the WS-Security, WS-Trust, and WS-SecureConversation security specifications.

System.Object
  System.IdentityModel.Selectors.SecurityTokenSerializer
    System.ServiceModel.Security.WSSecurityTokenSerializer

Namespace:  System.ServiceModel.Security
Assembly:  System.ServiceModel (in System.ServiceModel.dll)
public class WSSecurityTokenSerializer : SecurityTokenSerializer

The WSSecurityTokenSerializer type exposes the following members.

  Name Description
Public method WSSecurityTokenSerializer() Initializes a new instance of the WSSecurityTokenSerializer class.
Public method WSSecurityTokenSerializer(Boolean) Initializes a new instance of the WSSecurityTokenSerializer class.
Public method WSSecurityTokenSerializer(SecurityVersion) Initializes a new instance of the WSSecurityTokenSerializer class.
Public method WSSecurityTokenSerializer(SecurityVersion, Boolean) Initializes a new instance of the WSSecurityTokenSerializer class.
Public method WSSecurityTokenSerializer(SecurityVersion, Boolean, SamlSerializer) Initializes a new instance of the WSSecurityTokenSerializer class.
Public method WSSecurityTokenSerializer(SecurityVersion, Boolean, SamlSerializer, SecurityStateEncoder, IEnumerable<Type>) Initializes a new instance of the WSSecurityTokenSerializer class.
Public method WSSecurityTokenSerializer(SecurityVersion, TrustVersion, SecureConversationVersion, Boolean, SamlSerializer, SecurityStateEncoder, IEnumerable<Type>) Initializes a new instance of the WSSecurityTokenSerializer class.
Public method WSSecurityTokenSerializer(SecurityVersion, Boolean, SamlSerializer, SecurityStateEncoder, IEnumerable<Type>, Int32, Int32, Int32) Initializes a new instance of the WSSecurityTokenSerializer class.
Public method WSSecurityTokenSerializer(SecurityVersion, TrustVersion, SecureConversationVersion, Boolean, SamlSerializer, SecurityStateEncoder, IEnumerable<Type>, Int32, Int32, Int32) Initializes a new instance of the WSSecurityTokenSerializer class.
Top
  Name Description
Public property Static member DefaultInstance Gets a default instance of this class.
Public property EmitBspRequiredAttributes Gets a value that indicates whether to emit BSP required attributes.
Public property MaximumKeyDerivationLabelLength Gets the maximum key derivation label length.
Public property MaximumKeyDerivationNonceLength Gets the maximum key derivation nonce length.
Public property MaximumKeyDerivationOffset Gets the maximum key derivation offset.
Public property SecurityVersion Gets the security version.
Top
  Name Description
Public method CanReadKeyIdentifier Determines whether this serializer can read the <KeyIdentifier> element referred to by the specified XML reader. (Inherited from SecurityTokenSerializer.)
Public method CanReadKeyIdentifierClause Determines whether this serializer can read a clause in a <KeyIdentifier> element referred to by the specified XML reader. (Inherited from SecurityTokenSerializer.)
Protected method CanReadKeyIdentifierClauseCore Gets a value that indicates whether the key identifier clause core can be read using the specified reader. (Overrides SecurityTokenSerializer.CanReadKeyIdentifierClauseCore(XmlReader).)
Protected method CanReadKeyIdentifierCore Gets a value that indicates whether the key identifier core can be read using the specified reader. (Overrides SecurityTokenSerializer.CanReadKeyIdentifierCore(XmlReader).)
Public method CanReadToken Determines whether this serializer can read the security token pointed at by the specified XML reader. (Inherited from SecurityTokenSerializer.)
Protected method CanReadTokenCore Gets a value that indicates whether the token core can be read using the specified reader. (Overrides SecurityTokenSerializer.CanReadTokenCore(XmlReader).)
Public method CanWriteKeyIdentifier Determines whether this serializer can write the specified key identifier. (Inherited from SecurityTokenSerializer.)
Public method CanWriteKeyIdentifierClause Determines whether this serializer can write the specified key identifier clause. (Inherited from SecurityTokenSerializer.)
Protected method CanWriteKeyIdentifierClauseCore Gets a value that indicates whether the key identifier clause core with a specified key identifier clause can be written. (Overrides SecurityTokenSerializer.CanWriteKeyIdentifierClauseCore(SecurityKeyIdentifierClause).)
Protected method CanWriteKeyIdentifierCore Gets a value that indicates whether the key identifier core with a specified key identifier clause can be written. (Overrides SecurityTokenSerializer.CanWriteKeyIdentifierCore(SecurityKeyIdentifier).)
Public method CanWriteToken Determines whether this serializer can write the specified security token to XML. (Inherited from SecurityTokenSerializer.)
Protected method CanWriteTokenCore Gets a value that indicates whether the token core with a specified security token can be written. (Overrides SecurityTokenSerializer.CanWriteTokenCore(SecurityToken).)
Public method CreateKeyIdentifierClauseFromTokenXml Creates a key identifier clause from a token expressed as XML.
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Protected method GetTokenTypeUri Gets the URI of the security token of the specified type.
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ReadKeyIdentifier Reads the key identifier using specified XML reader. (Inherited from SecurityTokenSerializer.)
Public method ReadKeyIdentifierClause Reads the key identifier clause using specified XML reader. (Inherited from SecurityTokenSerializer.)
Protected method ReadKeyIdentifierClauseCore Reads the key identifier clause core using the specified XmlReader. (Overrides SecurityTokenSerializer.ReadKeyIdentifierClauseCore(XmlReader).)
Protected method ReadKeyIdentifierCore Reads the key identifier core using the specified XmlReader. (Overrides SecurityTokenSerializer.ReadKeyIdentifierCore(XmlReader).)
Public method ReadToken Reads the security token pointed at by the specified XML reader. (Inherited from SecurityTokenSerializer.)
Protected method ReadTokenCore Reads the token core using the specified XmlReader. (Overrides SecurityTokenSerializer.ReadTokenCore(XmlReader, SecurityTokenResolver).)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Public method TryCreateKeyIdentifierClauseFromTokenXml Attempts to create a key identifier clause from a token expressed as XML.
Public method WriteKeyIdentifier Writes the specified key identifier using the specified XML writer. (Inherited from SecurityTokenSerializer.)
Public method WriteKeyIdentifierClause Writes the specified key identifier clause using the specified XML writer. (Inherited from SecurityTokenSerializer.)
Protected method WriteKeyIdentifierClauseCore Writes the key identifier clause core with a specified key identifier clause using the specified XmlWriter. (Overrides SecurityTokenSerializer.WriteKeyIdentifierClauseCore(XmlWriter, SecurityKeyIdentifierClause).)
Protected method WriteKeyIdentifierCore Writes the key identifier core with a specified key identifier clause using the specified XmlWriter. (Overrides SecurityTokenSerializer.WriteKeyIdentifierCore(XmlWriter, SecurityKeyIdentifier).)
Public method WriteToken Writes the specified security token using the specified XML writer. (Inherited from SecurityTokenSerializer.)
Protected method WriteTokenCore Writes the token core with a specified security token using the specified XmlWriter. (Overrides SecurityTokenSerializer.WriteTokenCore(XmlWriter, SecurityToken).)
Top

The following code demonstrates a custom override of this class.


public class CreditCardSecurityTokenSerializer : WSSecurityTokenSerializer
{
    public CreditCardSecurityTokenSerializer(SecurityTokenVersion version) : base() { }

    protected override bool CanReadTokenCore(XmlReader reader)
    {
        XmlDictionaryReader localReader = XmlDictionaryReader.CreateDictionaryReader(reader);
        if (reader == null)
        {
            throw new ArgumentNullException("reader");
        }
        if (reader.IsStartElement(Constants.CreditCardTokenName, Constants.CreditCardTokenNamespace))
        {
            return true;
        }
        return base.CanReadTokenCore(reader);
    }

    protected override SecurityToken ReadTokenCore(XmlReader reader, SecurityTokenResolver tokenResolver)
    {
        if (reader == null)
        {
            throw new ArgumentNullException("reader");
        }
        if (reader.IsStartElement(Constants.CreditCardTokenName, Constants.CreditCardTokenNamespace))
        {
            string id = reader.GetAttribute(Constants.Id, Constants.WsUtilityNamespace);

            reader.ReadStartElement();

            // Read the credit card number.
            string creditCardNumber = reader.ReadElementString(Constants.CreditCardNumberElementName, Constants.CreditCardTokenNamespace);

            // Read the expiration date.
            string expirationTimeString = reader.ReadElementString(Constants.CreditCardExpirationElementName, Constants.CreditCardTokenNamespace);
            DateTime expirationTime = XmlConvert.ToDateTime(expirationTimeString, XmlDateTimeSerializationMode.Utc);

            // Read the issuer of the credit card.
            string creditCardIssuer = reader.ReadElementString(Constants.CreditCardIssuerElementName, Constants.CreditCardTokenNamespace);
            reader.ReadEndElement();

            CreditCardInfo cardInfo = new CreditCardInfo(creditCardNumber, creditCardIssuer, expirationTime);

            return new CreditCardToken(cardInfo, id);
        }
        else
        {
            return WSSecurityTokenSerializer.DefaultInstance.ReadToken(reader, tokenResolver);
        }
    }

    protected override bool CanWriteTokenCore(SecurityToken token)
    {
        if (token is CreditCardToken)
        {
            return true;
        }
        else
        {
            return base.CanWriteTokenCore(token);
        }
    }

    protected override void WriteTokenCore(XmlWriter writer, SecurityToken token)
    {
        if (writer == null) 
        { 
            throw new ArgumentNullException("writer"); 
        }
        if (token == null) 
        { 
            throw new ArgumentNullException("token"); 
        }

        CreditCardToken c = token as CreditCardToken;
        if (c != null)
        {
            writer.WriteStartElement(Constants.CreditCardTokenPrefix, Constants.CreditCardTokenName, Constants.CreditCardTokenNamespace);
            writer.WriteAttributeString(Constants.WsUtilityPrefix, Constants.Id, Constants.WsUtilityNamespace, token.Id);
            writer.WriteElementString(Constants.CreditCardNumberElementName, Constants.CreditCardTokenNamespace, c.CardInfo.CardNumber);
            writer.WriteElementString(Constants.CreditCardExpirationElementName, Constants.CreditCardTokenNamespace, XmlConvert.ToString(c.CardInfo.ExpirationDate, XmlDateTimeSerializationMode.Utc));
            writer.WriteElementString(Constants.CreditCardIssuerElementName, Constants.CreditCardTokenNamespace, c.CardInfo.CardIssuer);
            writer.WriteEndElement();
            writer.Flush();
        }
        else
        {
            base.WriteTokenCore(writer, token);
        }
    }
}


.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ