SecurityToken クラス

定義

すべてのセキュリティ トークンを実装するために使用される基本クラスを表します。

public ref class SecurityToken abstract
public abstract class SecurityToken
type SecurityToken = class
Public MustInherit Class SecurityToken
継承
SecurityToken
派生

トピックで SecurityToken 使用されるコード例は、サンプルから取得します Custom Token 。 このサンプルでは、Simple Web Tokens (SWT) の処理を有効にするカスタム クラスを提供します。 これには、クラスとクラスの SimpleWebToken 実装、および SWT トークンを SimpleWebTokenHandler サポートする他のクラスが含まれます。 WIF で使用できるこのサンプルとその他のサンプルの詳細と、それらをダウンロードする場所については、「 WIF コード サンプル インデックス」を参照してください。 次のコードは、 クラスの実装を SimpleWebToken 示しています。 このクラスは を拡張します SecurityToken

/// <summary>
/// Defines the set of constants for the Simple Web Token.
/// </summary>
public static class SimpleWebTokenConstants
{
    public const string Audience = "Audience";
    public const string ExpiresOn = "ExpiresOn";
    public const string Id = "Id";
    public const string Issuer = "Issuer";
    public const string Signature = "HMACSHA256";
    public const string ValidFrom = "ValidFrom";
    public const string ValueTypeUri = "http://schemas.xmlsoap.org/ws/2009/11/swt-token-profile-1.0";     
}
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.IdentityModel.Tokens;

namespace SimpleWebToken
{
    /// <summary>
    /// This class represents the token format for the SimpleWebToken.
    /// </summary>
    public class SimpleWebToken : SecurityToken
    {
        public static DateTime SwtBaseTime = new DateTime( 1970, 1, 1, 0, 0, 0, 0 ); // per SWT psec

        NameValueCollection _properties;
        string _serializedToken;

        /// <summary>
        /// Initializes a new instance of the <see cref="SimpleWebToken"/> class.
        /// This is an internal constructor that is only called from the <see cref="SimpleWebTokenHandler"/> when reading a token received from the wire.
        /// </summary>
        /// <param name="properties">The collection representing all the key value pairs in the token.</param>
        /// <param name="serializedToken">The serialized form of the token.</param>
        internal SimpleWebToken( NameValueCollection properties, string serializedToken )
            : this(properties)
        {
            _serializedToken = serializedToken;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SimpleWebToken"/> class.
        /// </summary>
        /// <param name="properties">The collection representing all the key value pairs in the token.</param>
        public SimpleWebToken( NameValueCollection properties )
        {
            if ( properties == null )
            {
                throw new ArgumentNullException( "properties" );
            }

            _properties = properties;
        }

        /// <summary>
        /// Gets the Id of the token.
        /// </summary>
        /// <value>The Id of the token.</value>
        public override string Id
        {
            get 
            {
                return _properties[SimpleWebTokenConstants.Id];
            }
        }

        /// <summary>
        /// Gets the keys associated with this token.
        /// </summary>
        /// <value>The keys associated with this token.</value>
        public override ReadOnlyCollection<SecurityKey> SecurityKeys
        {
            get 
            { 
                return new ReadOnlyCollection<SecurityKey>( new List<SecurityKey>() ); 
            }
        }

        /// <summary>
        /// Gets the time from when the token is valid.
        /// </summary>
        /// <value>The time from when the token is valid.</value>
        public override DateTime ValidFrom
        {
            get 
            {
                string validFrom = _properties[SimpleWebTokenConstants.ValidFrom];
                return GetTimeAsDateTime( String.IsNullOrEmpty( validFrom ) ? "0" : validFrom );
            }
        }

        /// <summary>
        /// Gets the time when the token expires.
        /// </summary>
        /// <value>The time up to which the token is valid.</value>
        public override DateTime ValidTo
        {
            get
            {
                string expiryTime = _properties[SimpleWebTokenConstants.ExpiresOn];
                return GetTimeAsDateTime( String.IsNullOrEmpty( expiryTime ) ? "0" : expiryTime );
            }
        }

        /// <summary>
        /// Gets the Audience for the token.
        /// </summary>
        /// <value>The audience of the token.</value>
        public string Audience
        {
            get 
            {
                return _properties[SimpleWebTokenConstants.Audience];
            }
        }

        /// <summary>
        /// Gets the Issuer for the token.
        /// </summary>
        /// <value>The issuer for the token.</value>
        public string Issuer
        {
            get 
            { 
                return _properties[SimpleWebTokenConstants.Issuer]; 
            }
        }

        /// <summary>
        /// Gets the signature for the token.
        /// </summary>
        /// <value>The signature for the token.</value>
        public string Signature
        {
            get 
            { 
                return _properties[SimpleWebTokenConstants.Signature]; 
            }
        }

        /// <summary>
        /// Gets the serialized form of the token if the token was created from its serialized form by the token handler.
        /// </summary>
        /// <value>The serialized form of the token.</value>
        public string SerializedToken
        {
            get
            {
                return _serializedToken;
            }
        }

        /// <summary>
        /// Creates a copy of all key value pairs of the token.
        /// </summary>
        /// <returns>A copy of all the key value pairs in the token.</returns>
        public NameValueCollection GetAllProperties()
        {
            return new NameValueCollection( _properties );
        }

        /// <summary>
        /// Converts the time in seconds to a <see cref="DateTime"/> object based on the base time 
        /// defined by the Simple Web Token.
        /// </summary>
        /// <param name="expiryTime">The time in seconds.</param>
        /// <returns>The time as a <see cref="DateTime"/> object.</returns>
        protected virtual DateTime GetTimeAsDateTime( string expiryTime )
        {
            long totalSeconds = 0;
            if ( !long.TryParse( expiryTime, out totalSeconds ) )
            {
                throw new SecurityTokenException("Invalid expiry time. Expected the time to be in seconds passed from 1 January 1970.");
            }

            long maxSeconds = (long)( DateTime.MaxValue - SwtBaseTime ).TotalSeconds - 1;
            if ( totalSeconds > maxSeconds )
            {
                totalSeconds = maxSeconds;
            }

            return SwtBaseTime.AddSeconds( totalSeconds );
        } 
    }    
}

注釈

セキュリティ トークンを使用して、認証資格情報を提供するか、メッセージを保護します。

セキュリティ トークンは、認証資格情報、暗号化キーマテリアル、またはセキュリティ トークン サービス (STS) によって発行されたセキュリティ トークンの場合は、サブジェクトに関するクレームのコレクションを提供するために使用できます。 すべてのセキュリティ トークンは、 クラスから SecurityToken 派生します。

.NET 4.5 以降、Windows Identity Foundation (WIF) は .NET Frameworkに完全に統合されており、WIF によって公開されるクラスは、コードでセキュリティ トークンを処理する推奨される方法です。 WIF では、セキュリティ トークンは XML 表現との間でシリアル化および逆シリアル化され、基底クラスから派生したクラスを SecurityTokenHandler 使用して検証されます。 トークンの検証には、トークンが有効であることを確認するだけでなく、認証と承認の決定に使用できるトークンからインスタンスを返 ClaimsIdentity すことも含まれます。 ClaimsIdentityは、トークンに含まれる要求と、トークン型ValidateToken自体に組み込まれている要求からの メソッドのトークン ハンドラーの実装によって構築されます。

WIF には、次の種類のセキュリティ トークンがサポートされています。

  • Saml2SecurityToken: SAML 2.0 アサーションに基づくセキュリティ トークンを表します。 通常、このトークンの種類は、WS-Trust または WS-Federation セキュリティ トークン要求 (RST) に応答してセキュリティ トークン サービスによって発行されます。

  • SamlSecurityToken: SAML 1.1 アサーションに基づくセキュリティ トークンを表します。 通常、このトークンの種類は、WS-Trust または WS-Federation セキュリティ トークン要求 (RST) に応答してセキュリティ トークン サービスによって発行されます。

  • KerberosRequestorSecurityToken および KerberosReceiverSecurityToken: SOAP メッセージで受信または送信される Kerberos チケットに基づくセキュリティ トークンを表します

  • RsaSecurityToken: RSA アルゴリズムを使用して作成されたキーに基づくセキュリティ トークンを表します。

  • SessionSecurityToken: セッションに関する情報を含むセキュリティ トークンを表します。

  • UserNameSecurityToken: ユーザー名とパスワードに基づくセキュリティ トークンを表します。

  • WindowsSecurityToken: Windows ドメインまたはユーザー アカウントの ID に基づくセキュリティ トークンを表します。

  • X509SecurityToken: X.509 証明書に基づくセキュリティ トークンを表します。

  • X509WindowsSecurityToken: Windows ドメイン ユーザーまたはローカル コンピューター ユーザー アカウントにマップされる X.509 証明書に基づくセキュリティ トークンを表します。

他の 2 つのセキュリティ トークン クラス GenericXmlSecurityTokenEncryptedSecurityTokenを使用して、一般的なケースを処理できます。

大まかに言えば、セキュリティ トークンは次の 3 つの主要なカテゴリに分類されます。

  • 暗号化キーマテリアルを保持または参照するトークン。 たとえば、 RsaSecurityToken 型と X509SecurityToken 型は、多くの場合、この目的で使用されます。

  • 既に認証されているユーザーの資格情報を表すトークン。 たとえば、、UserNameSecurityTokenWindowsSecurityToken、および は、証明書を使用して認証されたユーザーの場合は、 型ですX509SecurityToken

  • WS-Trust または WS-Federation プロトコルを使用してセキュリティ トークン要求に応答してセキュリティ トークン サービス (STS) によって発行されるトークン。 これらは通常、XML フラグメントで wst:RequestSecurityTokenResponse 返されます。 Saml2SecurityToken型と SamlSecurityToken 型は、ほとんどの場合、これらのトークンを表すために使用されます。

特殊なトークンの種類 である には SessionSecurityToken、アクティブまたはパッシブのシナリオでセッションを使用するときにプリンシパルを再作成するために必要な情報が含まれています。

既存のトークン型に機能を追加するには、特定の型とそれに関連付けられているトークン ハンドラーから派生して、トークンに追加する新しい要素をサポートできます。 新しいトークン型のサポートを追加するには、 クラスから SecurityToken 直接派生できます。 これを行う場合は、 クラスから SecurityTokenHandler 派生してトークン ハンドラー クラスを作成する必要もあります。 トークンの使用方法によっては、 クラスから派生してカスタム トークン リゾルバーを作成する必要がある場合と、 クラスから IssuerTokenResolver 派生して 1 つ以上のカスタム キー識別子句の型を SecurityKeyIdentifierClause 作成する必要がある場合もあります。

注意 (実装者)

SecurityKeysValidFrom、、および の各プロパティをIdオーバーライドするValidTo必要があります。 、、、および ResolveKeyIdentifierClause(SecurityKeyIdentifierClause) メソッドはすべてCanCreateKeyIdentifierClause<T>()、 型LocalIdKeyIdentifierClauseのキー識別子をサポートMatchesKeyIdentifierClause(SecurityKeyIdentifierClause)CreateKeyIdentifierClause<T>()します。 派生クラスの他のキー識別子型をサポートするには、これらのメソッドをオーバーライドする必要があります。

コンストラクター

SecurityToken()

SecurityToken クラスを初期化するために、派生クラスのコンストラクターによって呼び出されます。

プロパティ

Id

セキュリティ トークンの一意の識別子を取得します。

SecurityKeys

セキュリティ トークンに関連付けられた暗号化キーを取得します。

ValidFrom

このセキュリティ トークンの有効期間の開始時点を取得します。

ValidTo

このセキュリティ トークンの有効期間の終了時点を取得します。

メソッド

CanCreateKeyIdentifierClause<T>()

このセキュリティ トークンが、指定したキー識別子を作成できるかどうかを示す値を取得します。

CreateKeyIdentifierClause<T>()

指定したキー識別句を作成します。

Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
MatchesKeyIdentifierClause(SecurityKeyIdentifierClause)

このインスタンスのキー識別子を指定したキー識別子に解決できるかどうかを示す値を返します。

MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
ResolveKeyIdentifierClause(SecurityKeyIdentifierClause)

指定したキー識別句のキーを取得します。

ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

適用対象

こちらもご覧ください