WSSecurityTokenSerializer 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
一个类,它可用于序列化和反序列化在 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
- 继承
示例
下面的代码演示此类的自定义重写。
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
构造函数
属性
DefaultInstance |
获取此类的默认实例。 |
EmitBspRequiredAttributes |
获取一个值,该值指示是否发出 BSP 所需的属性。 |
MaximumKeyDerivationLabelLength |
获取最大密钥派生标签长度。 |
MaximumKeyDerivationNonceLength |
获取最大密钥派生 Nonce 长度。 |
MaximumKeyDerivationOffset |
获取最大密钥派生偏移量。 |
SecurityVersion |
获取安全版本。 |