Share via


SecurityToken.ValidTo プロパティ

定義

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

public:
 abstract property DateTime ValidTo { DateTime get(); };
public abstract DateTime ValidTo { get; }
member this.ValidTo : DateTime
Public MustOverride ReadOnly Property ValidTo As DateTime

プロパティ値

このセキュリティ トークンの有効期間の終了の時点を示す DateTime

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

/// <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";     
}
public static DateTime SwtBaseTime = new DateTime( 1970, 1, 1, 0, 0, 0, 0 ); // per SWT psec

NameValueCollection _properties;
/// <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>
/// 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 );
}

注釈

ValidFrom および ValidTo プロパティを使用して、SecurityToken トークンの有効期間を決定します。 ValidFrom および ValidTo プロパティはそれぞれ、セキュリティ トークンの有効期間の開始と終了の時点を示します。

注意 (実装者)

プロパティをオーバーライドする ValidTo 必要があります。

適用対象