WSSecurityTokenSerializer 类

定义

一个类,它可用于序列化和反序列化在 WS-Security、WS-Trust 以及 WS-SecureConversation 安全规范中定义的安全令牌、安全密钥标识符和安全密钥标识符子句。

public ref class WSSecurityTokenSerializer : System::IdentityModel::Selectors::SecurityTokenSerializer
public class WSSecurityTokenSerializer : System.IdentityModel.Selectors.SecurityTokenSerializer
type WSSecurityTokenSerializer = class
    inherit SecurityTokenSerializer
Public Class WSSecurityTokenSerializer
Inherits SecurityTokenSerializer
继承
WSSecurityTokenSerializer

示例

下面的代码演示此类的自定义重写。

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);
        }
    }
}
Public Class CreditCardSecurityTokenSerializer
    Inherits WSSecurityTokenSerializer

    Public Sub New(ByVal version As SecurityTokenVersion)
        MyBase.New()
    End Sub

    Protected Overrides Function CanReadTokenCore(ByVal reader As XmlReader) As Boolean
        Dim localReader = XmlDictionaryReader.CreateDictionaryReader(reader)
        If reader Is Nothing Then
            Throw New ArgumentNullException("reader")
        End If
        If reader.IsStartElement(Constants.CreditCardTokenName, _
                                 Constants.CreditCardTokenNamespace) Then
            Return True
        End If
        Return MyBase.CanReadTokenCore(reader)
    End Function

    Protected Overrides Function ReadTokenCore(ByVal reader As XmlReader, _
                                               ByVal tokenResolver As SecurityTokenResolver) As SecurityToken
        If reader Is Nothing Then
            Throw New ArgumentNullException("reader")
        End If
        If reader.IsStartElement(Constants.CreditCardTokenName, _
                                 Constants.CreditCardTokenNamespace) Then

            Dim id = reader.GetAttribute(Constants.Id, _
                                         Constants.WsUtilityNamespace)
            reader.ReadStartElement()

            ' Read the credit card number.
            Dim creditCardNumber = reader.ReadElementString(Constants.CreditCardNumberElementName, _
                                                            Constants.CreditCardTokenNamespace)

            ' Read the expiration date.
            Dim expirationTimeString = reader.ReadElementString(Constants.CreditCardExpirationElementName, _
                                                                Constants.CreditCardTokenNamespace)
            Dim expirationTime As DateTime = XmlConvert.ToDateTime(expirationTimeString, _
                                                                   XmlDateTimeSerializationMode.Utc)

            ' Read the issuer of the credit card.
            Dim creditCardIssuer = reader.ReadElementString(Constants.CreditCardIssuerElementName, _
                                                            Constants.CreditCardTokenNamespace)
            reader.ReadEndElement()

            Dim cardInfo As New CreditCardInfo(creditCardNumber, _
                                               creditCardIssuer, _
                                               expirationTime)

            Return New CreditCardToken(cardInfo, id)
        Else
            Return WSSecurityTokenSerializer.DefaultInstance.ReadToken(reader, _
                                                                       tokenResolver)
        End If
    End Function

    Protected Overrides Function CanWriteTokenCore(ByVal token As SecurityToken) As Boolean
        If TypeOf token Is CreditCardToken Then
            Return True
        Else
            Return MyBase.CanWriteTokenCore(token)
        End If
    End Function

    Protected Overrides Sub WriteTokenCore(ByVal writer As XmlWriter, _
                                           ByVal token As SecurityToken)
        If writer Is Nothing Then
            Throw New ArgumentNullException("writer")
        End If
        If token Is Nothing Then
            Throw New ArgumentNullException("token")
        End If

        Dim c = TryCast(token, CreditCardToken)
        If c IsNot Nothing Then
            With writer
                .WriteStartElement(Constants.CreditCardTokenPrefix, _
                                   Constants.CreditCardTokenName, _
                                   Constants.CreditCardTokenNamespace)
                .WriteAttributeString(Constants.WsUtilityPrefix, _
                                      Constants.Id, _
                                      Constants.WsUtilityNamespace, _
                                      token.Id)
                .WriteElementString(Constants.CreditCardNumberElementName, _
                                    Constants.CreditCardTokenNamespace, _
                                    c.CardInfo.CardNumber)
                .WriteElementString(Constants.CreditCardExpirationElementName, _
                                    Constants.CreditCardTokenNamespace, _
                                    XmlConvert.ToString(c.CardInfo.ExpirationDate, _
                                                        XmlDateTimeSerializationMode.Utc))
                .WriteElementString(Constants.CreditCardIssuerElementName, _
                                    Constants.CreditCardTokenNamespace, _
                                    c.CardInfo.CardIssuer)
                .WriteEndElement()
                .Flush()
            End With
        Else
            MyBase.WriteTokenCore(writer, token)
        End If
    End Sub

End Class

构造函数

WSSecurityTokenSerializer()

初始化 WSSecurityTokenSerializer 类的新实例。

WSSecurityTokenSerializer(Boolean)

初始化 WSSecurityTokenSerializer 类的新实例。

WSSecurityTokenSerializer(SecurityVersion)

初始化 WSSecurityTokenSerializer 类的新实例。

WSSecurityTokenSerializer(SecurityVersion, Boolean)

初始化 WSSecurityTokenSerializer 类的新实例。

WSSecurityTokenSerializer(SecurityVersion, Boolean, SamlSerializer)

初始化 WSSecurityTokenSerializer 类的新实例。

WSSecurityTokenSerializer(SecurityVersion, Boolean, SamlSerializer, SecurityStateEncoder, IEnumerable<Type>)

初始化 WSSecurityTokenSerializer 类的新实例。

WSSecurityTokenSerializer(SecurityVersion, Boolean, SamlSerializer, SecurityStateEncoder, IEnumerable<Type>, Int32, Int32, Int32)

初始化 WSSecurityTokenSerializer 类的新实例。

WSSecurityTokenSerializer(SecurityVersion, TrustVersion, SecureConversationVersion, Boolean, SamlSerializer, SecurityStateEncoder, IEnumerable<Type>)

初始化 WSSecurityTokenSerializer 类的新实例。

WSSecurityTokenSerializer(SecurityVersion, TrustVersion, SecureConversationVersion, Boolean, SamlSerializer, SecurityStateEncoder, IEnumerable<Type>, Int32, Int32, Int32)

初始化 WSSecurityTokenSerializer 类的新实例。

属性

DefaultInstance

获取此类的默认实例。

EmitBspRequiredAttributes

获取一个值,该值指示是否发出 BSP 所需的属性。

MaximumKeyDerivationLabelLength

获取最大密钥派生标签长度。

MaximumKeyDerivationNonceLength

获取最大密钥派生 Nonce 长度。

MaximumKeyDerivationOffset

获取最大密钥派生偏移量。

SecurityVersion

获取安全版本。

方法

CanReadKeyIdentifier(XmlReader)

确定此序列化程序是否可读取由指定的 XML 读取器引用的<KeyIdentifier> 元素。

(继承自 SecurityTokenSerializer)
CanReadKeyIdentifierClause(XmlReader)

确定此序列化程序是否可读取由指定的 XML 读取器引用的<KeyIdentifier> 元素中的子句。

(继承自 SecurityTokenSerializer)
CanReadKeyIdentifierClauseCore(XmlReader)

获取一个值,该值指示密钥标识符子句核心是否可以使用指定的读取器读取。

CanReadKeyIdentifierCore(XmlReader)

获取一个值,该值指示密钥标识符核心是否可以使用指定的读取器读取。

CanReadToken(XmlReader)

确定此序列化程序是否可读取由指定的 XML 读取器指向的安全令牌。

(继承自 SecurityTokenSerializer)
CanReadTokenCore(XmlReader)

获取一个值,该值指示令牌核心是否可以使用指定的读取器读取。

CanWriteKeyIdentifier(SecurityKeyIdentifier)

确定此序列化程序是否可以编写指定的密钥标识符。

(继承自 SecurityTokenSerializer)
CanWriteKeyIdentifierClause(SecurityKeyIdentifierClause)

确定此序列化程序是否可以编写指定的密钥标识符子句。

(继承自 SecurityTokenSerializer)
CanWriteKeyIdentifierClauseCore(SecurityKeyIdentifierClause)

获取一个值,该值指示是否可以写入带有指定密钥标识符子句的密钥标识符子句核心。

CanWriteKeyIdentifierCore(SecurityKeyIdentifier)

获取一个值,该值指示是否可以写入带有指定密钥标识符子句的密钥标识符核心。

CanWriteToken(SecurityToken)

确定此序列化程序是否可以将指定的安全令牌编写为 XML。

(继承自 SecurityTokenSerializer)
CanWriteTokenCore(SecurityToken)

获取一个值,该值指示是否可以写入带有指定安全令牌的令牌核心。

CreateKeyIdentifierClauseFromTokenXml(XmlElement, SecurityTokenReferenceStyle)

从以 XML 表示的令牌创建密钥标识符子句。

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetTokenTypeUri(Type)

获取指定类型的安全令牌的 URI。

GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ReadKeyIdentifier(XmlReader)

使用指定的 XML 读取器读取密钥标识符。

(继承自 SecurityTokenSerializer)
ReadKeyIdentifierClause(XmlReader)

使用指定的 XML 读取器读取密钥标识符子句。

(继承自 SecurityTokenSerializer)
ReadKeyIdentifierClauseCore(XmlReader)

使用指定的 XmlReader 读取密钥标识符子句核心。

ReadKeyIdentifierCore(XmlReader)

使用指定的 XmlReader 读取密钥标识符核心。

ReadToken(XmlReader, SecurityTokenResolver)

读取由指定的 XML 读取器指向的安全令牌。

(继承自 SecurityTokenSerializer)
ReadTokenCore(XmlReader, SecurityTokenResolver)

使用指定的 XmlReader 读取令牌核心。

ToString()

返回表示当前对象的字符串。

(继承自 Object)
TryCreateKeyIdentifierClauseFromTokenXml(XmlElement, SecurityTokenReferenceStyle, SecurityKeyIdentifierClause)

尝试从以 XML 表示的令牌创建密钥标识符子句。

WriteKeyIdentifier(XmlWriter, SecurityKeyIdentifier)

使用指定的 XML 编写器编写指定的密钥标识符。

(继承自 SecurityTokenSerializer)
WriteKeyIdentifierClause(XmlWriter, SecurityKeyIdentifierClause)

使用指定的 XML 编写器编写指定的密钥标识符子句。

(继承自 SecurityTokenSerializer)
WriteKeyIdentifierClauseCore(XmlWriter, SecurityKeyIdentifierClause)

使用指定的 XmlWriter 写入带有指定密钥标识符子句的密钥标识符子句核心。

WriteKeyIdentifierCore(XmlWriter, SecurityKeyIdentifier)

使用指定的 XmlWriter 写入带有指定密钥标识符子句的密钥标识符核心。

WriteToken(XmlWriter, SecurityToken)

使用指定的 XML 编写器编写指定的安全令牌。

(继承自 SecurityTokenSerializer)
WriteTokenCore(XmlWriter, SecurityToken)

使用指定的 XmlWriter 写入带有指定安全令牌的令牌核心。

适用于