SecurityTokenProvider Classe

Definizione

Rappresenta un provider di token di sicurezza che gestisce token di sicurezza per un mittente di messaggi SOAP.

public ref class SecurityTokenProvider abstract
public abstract class SecurityTokenProvider
type SecurityTokenProvider = class
Public MustInherit Class SecurityTokenProvider
Ereditarietà
SecurityTokenProvider
Derivato

Esempio

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

Commenti

Quando sono necessari token di sicurezza personalizzati, utilizzare la classe SecurityTokenProvider. Il ruolo di un provider del token di sicurezza è di ottenere un token di sicurezza quando un messaggio SOAP viene inviato da un client e viene utilizzato un token di sicurezza per autenticare il client o proteggere il messaggio SOAP. In particolare, il metodo GetToken viene chiamato per ottenere un token di sicurezza. Il provider di token di sicurezza può essere chiamato anche per annullare e rinnovare una sicurezza utilizzando i metodi CancelToken e RenewToken.

Le classi che derivano dalla classe SecurityTokenManager implementano il metodo CreateSecurityTokenProvider per determinare il provider di token di sicurezza necessario per un determinato token di sicurezza.

Le classi ClientCredentialsSecurityTokenManager e ServiceCredentialsSecurityTokenManager forniscono le implementazioni predefinite per i tipi di token di sicurezza incorporati. Negli scenari di token di sicurezza personalizzati è necessario derivare una classe da una delle classi SecurityTokenManager, ClientCredentialsSecurityTokenManager o ServiceCredentialsSecurityTokenManager e fornire la funzionalità per creare il provider, l'autenticatore e il serializzatore per il token di sicurezza personalizzato. Per altre informazioni sulla creazione di un token personalizzato, vedere Procedura: Creare un token personalizzato.

Costruttori

SecurityTokenProvider()

Inizializza una nuova istanza della classe SecurityTokenProvider.

Proprietà

SupportsTokenCancellation

Ottiene un valore che indica se il token di sicurezza può essere annullato.

SupportsTokenRenewal

Ottiene un valore che indica se il token di sicurezza può essere rinnovato.

Metodi

BeginCancelToken(TimeSpan, SecurityToken, AsyncCallback, Object)

Inizia un'operazione asincrona per annullare un token di sicurezza.

BeginCancelTokenCore(TimeSpan, SecurityToken, AsyncCallback, Object)

Inizia un'operazione asincrona per annullare un token di sicurezza.

BeginGetToken(TimeSpan, AsyncCallback, Object)

Inizia un'operazione asincrona per ottenere un token di sicurezza.

BeginGetTokenCore(TimeSpan, AsyncCallback, Object)

Inizia un'operazione asincrona per ottenere un token di sicurezza.

BeginRenewToken(TimeSpan, SecurityToken, AsyncCallback, Object)

Inizia un'operazione asincrona che rinnova un token di sicurezza.

BeginRenewTokenCore(TimeSpan, SecurityToken, AsyncCallback, Object)

Inizia un'operazione asincrona che rinnova un token di sicurezza.

CancelToken(TimeSpan, SecurityToken)

Consente di annullare un token di sicurezza.

CancelTokenAsync(TimeSpan, SecurityToken)

Consente di annullare un token di sicurezza.

CancelTokenCore(TimeSpan, SecurityToken)

Consente di annullare un token di sicurezza.

CancelTokenCoreAsync(TimeSpan, SecurityToken)

Consente di annullare un token di sicurezza.

EndCancelToken(IAsyncResult)

Completa un'operazione asincrona per annullare un token di sicurezza.

EndCancelTokenCore(IAsyncResult)

Completa un'operazione asincrona per annullare un token di sicurezza.

EndGetToken(IAsyncResult)

Completa un'operazione asincrona per ottenere un token di sicurezza.

EndGetTokenCore(IAsyncResult)

Completa un'operazione asincrona per ottenere un token di sicurezza.

EndRenewToken(IAsyncResult)

Completa un'operazione asincrona per rinnovare un token di sicurezza.

EndRenewTokenCore(IAsyncResult)

Completa un'operazione asincrona per rinnovare il token di sicurezza.

Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.

(Ereditato da Object)
GetHashCode()

Funge da funzione hash predefinita.

(Ereditato da Object)
GetToken(TimeSpan)

Ottiene un token di sicurezza .

GetTokenAsync(TimeSpan)

Ottiene un token di sicurezza .

GetTokenCore(TimeSpan)

Ottiene un token di sicurezza .

GetTokenCoreAsync(TimeSpan)

Ottiene un token di sicurezza .

GetType()

Ottiene l'oggetto Type dell'istanza corrente.

(Ereditato da Object)
MemberwiseClone()

Crea una copia superficiale dell'oggetto Object corrente.

(Ereditato da Object)
RenewToken(TimeSpan, SecurityToken)

Consente di rinnovare un token di sicurezza .

RenewTokenAsync(TimeSpan, SecurityToken)

Consente di rinnovare un token di sicurezza .

RenewTokenCore(TimeSpan, SecurityToken)

Consente di rinnovare un token di sicurezza .

RenewTokenCoreAsync(TimeSpan, SecurityToken)

Consente di rinnovare un token di sicurezza .

ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.

(Ereditato da Object)

Si applica a

Vedi anche