SecurityToken クラス
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
すべてのセキュリティ トークンを実装するために使用される基本クラスを表します。
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 クラスと SimpleWebTokenHandler クラスの実装と、SWT トークンをサポートする他のクラスが含まれます。 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 つのセキュリティ トークン クラス ( GenericXmlSecurityToken と EncryptedSecurityToken) を使用して、一般的なケースを処理できます。
大まかに言えば、セキュリティ トークンは次の 3 つの主要なカテゴリに分類されます。
暗号化キー マテリアルを伝達または参照するトークン。 たとえば、 RsaSecurityToken 型と X509SecurityToken 型は、この目的でよく使用されます。
既に認証されているユーザーの資格情報を表すトークン。 たとえば、 UserNameSecurityToken、 WindowsSecurityToken、および証明書を使用して認証されたユーザーの場合は、 X509SecurityToken 型です。
WS-Trust または WS-Federation プロトコルを使用してセキュリティ トークン要求に応答してセキュリティ トークン サービス (STS) によって発行されるトークン。 これらは通常、
wst:RequestSecurityTokenResponseXML フラグメントで返されます。 Saml2SecurityToken型とSamlSecurityToken型は、これらのトークンを表すために最もよく使用されます。
特殊なトークンの種類である SessionSecurityTokenには、アクティブまたはパッシブのシナリオでセッションを使用するときにプリンシパルを再作成するために必要な情報が含まれています。
既存のトークン型に機能を追加するには、特定の型とそれに関連付けられているトークン ハンドラーから派生して、トークンに追加する新しい要素をサポートできます。 新しいトークン型のサポートを追加するには、 SecurityToken クラスから直接派生できます。 これを行う場合は、 SecurityTokenHandler クラスから派生してトークン ハンドラー クラスを作成する必要もあります。 トークンの使用方法によっては、 IssuerTokenResolver クラスから派生したカスタム トークン リゾルバーと、 SecurityKeyIdentifierClause クラスから派生した 1 つ以上のカスタム キー識別子句の型を作成する必要がある場合もあります。
注意 (実装者)
Id、SecurityKeys、ValidFrom、およびValidToプロパティをオーバーライドする必要があります。 CanCreateKeyIdentifierClause<T>()、CreateKeyIdentifierClause<T>()、MatchesKeyIdentifierClause(SecurityKeyIdentifierClause)、およびResolveKeyIdentifierClause(SecurityKeyIdentifierClause)のメソッドはすべて、LocalIdKeyIdentifierClause型のキー識別子をサポートします。 派生クラスで他のキー識別子型をサポートするには、これらのメソッドをオーバーライドする必要があります。
コンストラクター
| 名前 | 説明 |
|---|---|
| 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) |