Share via


SecurityToken.ValidFrom 屬性

定義

取得這個安全性權杖有效的第一個瞬間。

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

屬性值

DateTime,表示這個安全性權杖有效的第一個瞬間。

範例

主題中使用的 SecurityToken 程式代碼範例取自 Custom Token 範例。 此範例提供自定義類別,可讓您處理簡單的 Web 令牌 (SWT) 。 它包含類別和SimpleWebTokenHandler類別的SimpleWebToken實作,以及支援 SWT 令牌的其他類別。 如需適用於 WIF 的這個範例和其他範例的相關信息,以及下載這些範例的位置,請參閱 WIF 程式代碼範例索引。 下列程式代碼顯示 屬性的 ValidFrom 覆寫。

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

備註

使用 ValidFromValidTo 屬性,即可判定 SecurityToken 權杖有效的期間。 ValidFromValidTo 屬性分別代表安全性權杖有效時的第一個和最後一個瞬間。

給實施者的注意事項

您必須覆寫 ValidFrom 屬性。

適用於