SecurityTokenProvider Classe

Définition

Représente un fournisseur de jetons de sécurité qui gère des jetons de sécurité pour un expéditeur de message SOAP.

public ref class SecurityTokenProvider abstract
public abstract class SecurityTokenProvider
type SecurityTokenProvider = class
Public MustInherit Class SecurityTokenProvider
Héritage
SecurityTokenProvider
Dérivé

Exemples

using System;

using System.IdentityModel.Selectors;
using System.IdentityModel.Tokens;

using System.IO;

using System.ServiceModel.Security;

using System.Xml;

namespace Microsoft.ServiceModel.Samples
{
    /// <summary>
    /// class that derives from SecurityTokenProvider and returns a SecurityToken representing a SAML assertion
    /// </summary>
    public class SamlSecurityTokenProvider : SecurityTokenProvider
    {
        /// <summary>
        /// The SAML assertion that the SamlSecurityTokenProvider will return as a SecurityToken
        /// </summary>
        SamlAssertion assertion;

        /// <summary>
        /// The proof token associated with the SAML assertion
        /// </summary>
        SecurityToken proofToken;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="assertion">The SAML assertion that the SamlSecurityTokenProvider will return as a SecurityToken</param>
        /// <param name="proofToken">The proof token associated with the SAML assertion</param>
        public SamlSecurityTokenProvider(SamlAssertion assertion, SecurityToken proofToken )
        {
            this.assertion = assertion;
            this.proofToken = proofToken;
        }

        /// <summary>
        /// Creates the security token
        /// </summary>
        /// <param name="timeout">Maximum amount of time the method is supposed to take. Ignored in this implementation.</param>
        /// <returns>A SecurityToken corresponding the SAML assertion and proof key specified at construction time</returns>
        protected override SecurityToken GetTokenCore(TimeSpan timeout)
        {
            // Create a SamlSecurityToken from the provided assertion
            SamlSecurityToken samlToken = new SamlSecurityToken(assertion);

            // Create a SecurityTokenSerializer that will be used to serialize the SamlSecurityToken
            WSSecurityTokenSerializer ser = new WSSecurityTokenSerializer();

            // Create a memory stream to write the serialized token into
            // Use an initial size of 64Kb
            MemoryStream s = new MemoryStream(UInt16.MaxValue);

            // Create an XmlWriter over the stream
            XmlWriter xw = XmlWriter.Create(s);

            // Write the SamlSecurityToken into the stream
            ser.WriteToken(xw, samlToken);

            // Seek back to the beginning of the stream
            s.Seek(0, SeekOrigin.Begin);

            // Load the serialized token into a DOM
            XmlDocument dom = new XmlDocument();
            dom.Load(s);

            // Create a KeyIdentifierClause for the SamlSecurityToken
            SamlAssertionKeyIdentifierClause samlKeyIdentifierClause = samlToken.CreateKeyIdentifierClause<SamlAssertionKeyIdentifierClause>();

            // Return a GenericXmlToken from the XML for the SamlSecurityToken, the proof token, the valid from
            // and valid until times from the assertion and the key identifier clause created above
            return new GenericXmlSecurityToken(dom.DocumentElement, proofToken, assertion.Conditions.NotBefore, assertion.Conditions.NotOnOrAfter, samlKeyIdentifierClause, samlKeyIdentifierClause, null);
        }
    }
}
Imports System.IdentityModel.Selectors
Imports System.IdentityModel.Tokens

Imports System.IO

Imports System.ServiceModel.Security

Imports System.Xml


'/ <summary>
'/ class that derives from SecurityTokenProvider and returns a SecurityToken representing a SAML assertion
'/ </summary>

Public Class SamlSecurityTokenProvider
    Inherits SecurityTokenProvider
    '/ <summary>
    '/ The SAML assertion that the SamlSecurityTokenProvider will return as a SecurityToken
    '/ </summary>
    Private assertion As SamlAssertion

    '/ <summary>
    '/ The proof token associated with the SAML assertion
    '/ </summary>
    Private proofToken As SecurityToken


    '/ <summary>
    '/ Constructor
    '/ </summary>
    '/ <param name="assertion">The SAML assertion that the SamlSecurityTokenProvider will return as a SecurityToken</param>
    '/ <param name="proofToken">The proof token associated with the SAML assertion</param>
    Public Sub New(ByVal assertion As SamlAssertion, ByVal proofToken As SecurityToken)
        Me.assertion = assertion
        Me.proofToken = proofToken

    End Sub


    '/ <summary>
    '/ Creates the security token
    '/ </summary>
    '/ <param name="timeout">Maximum amount of time the method is supposed to take. Ignored in this implementation.</param>
    '/ <returns>A SecurityToken corresponding the SAML assertion and proof key specified at construction time</returns>
    Protected Overrides Function GetTokenCore(ByVal timeout As TimeSpan) As SecurityToken
        ' Create a SamlSecurityToken from the provided assertion
        Dim samlToken As New SamlSecurityToken(assertion)

        ' Create a SecurityTokenSerializer that will be used to serialize the SamlSecurityToken
        Dim ser As New WSSecurityTokenSerializer()

        ' Create a memory stream to write the serialized token into
        ' Use an initial size of 64Kb
        Dim s As New MemoryStream(UInt16.MaxValue)

        ' Create an XmlWriter over the stream
        Dim xw As XmlWriter = XmlWriter.Create(s)

        ' Write the SamlSecurityToken into the stream
        ser.WriteToken(xw, samlToken)

        ' Seek back to the beginning of the stream
        s.Seek(0, SeekOrigin.Begin)

        ' Load the serialized token into a DOM
        Dim dom As New XmlDocument()
        dom.Load(s)

        ' Create a KeyIdentifierClause for the SamlSecurityToken
        Dim samlKeyIdentifierClause As SamlAssertionKeyIdentifierClause = samlToken.CreateKeyIdentifierClause(Of SamlAssertionKeyIdentifierClause)()
        
        ' Return a GenericXmlToken from the XML for the SamlSecurityToken, the proof token, the valid from 
        ' and valid until times from the assertion and the key identifier clause created above            
        Return New GenericXmlSecurityToken(dom.DocumentElement, proofToken, assertion.Conditions.NotBefore, assertion.Conditions.NotOnOrAfter, samlKeyIdentifierClause, samlKeyIdentifierClause, Nothing)

    End Function 'GetTokenCore
End Class

Remarques

Utilisez la classe SecurityTokenProvider lorsque des jetons de sécurité personnalisés sont requis. Le rôle d'un fournisseur de jetons de sécurité est d'obtenir un jeton de sécurité lorsqu'un message SOAP est envoyé par un client et qu'un jeton de sécurité est utilisé pour authentifier le client ou protéger le message SOAP. Plus précisément, la méthode GetToken est appelée pour obtenir un jeton de sécurité. Le fournisseur de jetons de sécurité peut également être appelé pour annuler et renouveler une sécurité à l'aide des méthodes CancelToken et RenewToken.

Les classes qui dérivent de la classe SecurityTokenManager implémentent la méthode CreateSecurityTokenProvider pour déterminer quel fournisseur de jetons de sécurité est requis pour un jeton de sécurité donné.

Les classes ClientCredentialsSecurityTokenManager et ServiceCredentialsSecurityTokenManager fournissent les implémentations par défaut pour les types de jetons de sécurité intégrés. Dans les scénarios de jetons de sécurité personnalisés, vous devez dériver une classe de l'une des classes SecurityTokenManager, ClientCredentialsSecurityTokenManager ou ServiceCredentialsSecurityTokenManager et programmer les fonctions chargées de créer le fournisseur de jetons de sécurité, l'authentificateur de jetons de sécurité et le sérialiseur de jeton de sécurité pour le jeton de sécurité personnalisé. Pour plus d’informations sur la création d’un jeton personnalisé, consultez Guide pratique pour créer un jeton personnalisé.

Constructeurs

SecurityTokenProvider()

Initialise une nouvelle instance de la classe SecurityTokenProvider.

Propriétés

SupportsTokenCancellation

Obtient une valeur qui indique si le jeton de sécurité peut être annulé.

SupportsTokenRenewal

Obtient une valeur qui indique si le jeton de sécurité est renouvelable.

Méthodes

BeginCancelToken(TimeSpan, SecurityToken, AsyncCallback, Object)

Commence une opération asynchrone pour annuler un jeton de sécurité.

BeginCancelTokenCore(TimeSpan, SecurityToken, AsyncCallback, Object)

Commence une opération asynchrone pour annuler un jeton de sécurité.

BeginGetToken(TimeSpan, AsyncCallback, Object)

Commence une opération asynchrone pour obtenir un jeton de sécurité.

BeginGetTokenCore(TimeSpan, AsyncCallback, Object)

Commence une opération asynchrone pour obtenir un jeton de sécurité.

BeginRenewToken(TimeSpan, SecurityToken, AsyncCallback, Object)

Commence une opération asynchrone qui renouvelle un jeton de sécurité.

BeginRenewTokenCore(TimeSpan, SecurityToken, AsyncCallback, Object)

Commence une opération asynchrone qui renouvelle un jeton de sécurité.

CancelToken(TimeSpan, SecurityToken)

Annule un jeton de sécurité.

CancelTokenAsync(TimeSpan, SecurityToken)

Annule un jeton de sécurité.

CancelTokenCore(TimeSpan, SecurityToken)

Annule un jeton de sécurité.

CancelTokenCoreAsync(TimeSpan, SecurityToken)

Annule un jeton de sécurité.

EndCancelToken(IAsyncResult)

Termine une opération asynchrone pour annuler un jeton de sécurité.

EndCancelTokenCore(IAsyncResult)

Termine une opération asynchrone pour annuler un jeton de sécurité.

EndGetToken(IAsyncResult)

Termine une opération asynchrone pour obtenir un jeton de sécurité.

EndGetTokenCore(IAsyncResult)

Termine une opération asynchrone pour obtenir un jeton de sécurité.

EndRenewToken(IAsyncResult)

Termine une opération asynchrone pour renouveler un jeton de sécurité.

EndRenewTokenCore(IAsyncResult)

Termine une opération asynchrone pour renouveler le jeton de sécurité.

Equals(Object)

Détermine si l'objet spécifié est égal à l'objet actuel.

(Hérité de Object)
GetHashCode()

Fait office de fonction de hachage par défaut.

(Hérité de Object)
GetToken(TimeSpan)

Obtient un jeton de sécurité.

GetTokenAsync(TimeSpan)

Obtient un jeton de sécurité.

GetTokenCore(TimeSpan)

Obtient un jeton de sécurité.

GetTokenCoreAsync(TimeSpan)

Obtient un jeton de sécurité.

GetType()

Obtient le Type de l'instance actuelle.

(Hérité de Object)
MemberwiseClone()

Crée une copie superficielle du Object actuel.

(Hérité de Object)
RenewToken(TimeSpan, SecurityToken)

Renouvelle un jeton de sécurité.

RenewTokenAsync(TimeSpan, SecurityToken)

Renouvelle un jeton de sécurité.

RenewTokenCore(TimeSpan, SecurityToken)

Renouvelle un jeton de sécurité.

RenewTokenCoreAsync(TimeSpan, SecurityToken)

Renouvelle un jeton de sécurité.

ToString()

Retourne une chaîne qui représente l'objet actuel.

(Hérité de Object)

S’applique à

Voir aussi