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 |
取得安全性版本。 |