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 。 此示例提供自定义类,这些类支持 (SWT) 处理简单 Web 令牌。 它包括类和 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 属性。

适用于