SecurityTokenProvider Klass

Definition

Representerar en säkerhetstokenprovider som hanterar säkerhetstoken för en SOAP-meddelandesändare.

public ref class SecurityTokenProvider abstract
public abstract class SecurityTokenProvider
type SecurityTokenProvider = class
Public MustInherit Class SecurityTokenProvider
Arv
SecurityTokenProvider
Härledda

Exempel

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

Kommentarer

SecurityTokenProvider Använd klassen när anpassade säkerhetstoken krävs. Rollen för en säkerhetstokenprovider är att hämta en säkerhetstoken när ett SOAP-meddelande skickas av en klient och en säkerhetstoken används för att autentisera klienten eller för att skydda SOAP-meddelandet. Mer specifikt anropas GetToken metoden för att hämta en säkerhetstoken. Providern för säkerhetstoken kan också anropas för att avbryta och förnya en säkerhet med hjälp av CancelToken metoderna och RenewToken .

Klasser som härleds från SecurityTokenManager klassen implementerar CreateSecurityTokenProvider metoden för att avgöra vilken säkerhetstokenprovider som krävs för en viss säkerhetstoken.

Klasserna ClientCredentialsSecurityTokenManager och ServiceCredentialsSecurityTokenManager tillhandahåller standardimplementeringar för inbyggda typer av säkerhetstoken. För scenarier med anpassade säkerhetstoken måste du härleda en klass från någon av SecurityTokenManagerklasserna , ClientCredentialsSecurityTokenManagereller ServiceCredentialsSecurityTokenManager och tillhandahålla funktionerna för att skapa providern för säkerhetstoken, autentiseringsnyckeln och serialiseraren för säkerhetstoken för den anpassade säkerhetstoken. Mer information om hur du skapar en anpassad token finns i Så här skapar du en anpassad token.

Konstruktorer

Name Description
SecurityTokenProvider()

Initierar en ny instans av SecurityTokenProvider klassen.

Egenskaper

Name Description
SupportsTokenCancellation

Hämtar ett värde som anger om säkerhetstoken kan avbrytas.

SupportsTokenRenewal

Hämtar ett värde som anger om säkerhetstoken kan förnyas.

Metoder

Name Description
BeginCancelToken(TimeSpan, SecurityToken, AsyncCallback, Object)

Påbörjar en asynkron åtgärd för att avbryta en säkerhetstoken.

BeginCancelTokenCore(TimeSpan, SecurityToken, AsyncCallback, Object)

Påbörjar en asynkron åtgärd för att avbryta en säkerhetstoken.

BeginGetToken(TimeSpan, AsyncCallback, Object)

Påbörjar en asynkron åtgärd för att hämta en säkerhetstoken.

BeginGetTokenCore(TimeSpan, AsyncCallback, Object)

Påbörjar en asynkron åtgärd för att hämta en säkerhetstoken.

BeginRenewToken(TimeSpan, SecurityToken, AsyncCallback, Object)

Påbörjar en asynkron åtgärd som förnyar en säkerhetstoken.

BeginRenewTokenCore(TimeSpan, SecurityToken, AsyncCallback, Object)

Påbörjar en asynkron åtgärd som förnyar en säkerhetstoken.

CancelToken(TimeSpan, SecurityToken)

Avbryter en säkerhetstoken.

CancelTokenCore(TimeSpan, SecurityToken)

Avbryter en säkerhetstoken.

EndCancelToken(IAsyncResult)

Slutför en asynkron åtgärd för att avbryta en säkerhetstoken.

EndCancelTokenCore(IAsyncResult)

Slutför en asynkron åtgärd för att avbryta en säkerhetstoken.

EndGetToken(IAsyncResult)

Slutför en asynkron åtgärd för att hämta en säkerhetstoken.

EndGetTokenCore(IAsyncResult)

Slutför en asynkron åtgärd för att hämta en säkerhetstoken.

EndRenewToken(IAsyncResult)

Slutför en asynkron åtgärd för att förnya en säkerhetstoken.

EndRenewTokenCore(IAsyncResult)

Slutför en asynkron åtgärd för att förnya säkerhetstoken.

Equals(Object)

Avgör om det angivna objektet är lika med det aktuella objektet.

(Ärvd från Object)
GetHashCode()

Fungerar som standard-hash-funktion.

(Ärvd från Object)
GetToken(TimeSpan)

Hämtar en säkerhetstoken.

GetTokenCore(TimeSpan)

Hämtar en säkerhetstoken.

GetType()

Hämtar den aktuella instansen Type .

(Ärvd från Object)
MemberwiseClone()

Skapar en ytlig kopia av den aktuella Object.

(Ärvd från Object)
RenewToken(TimeSpan, SecurityToken)

Förnyar en säkerhetstoken.

RenewTokenCore(TimeSpan, SecurityToken)

Förnyar en säkerhetstoken.

ToString()

Returnerar en sträng som representerar det aktuella objektet.

(Ärvd från Object)

Gäller för

Se även