/// <summary>
        /// Creates a security token based on a token descriptor.
        /// </summary>
        /// <param name="tokenDescriptor">The token descriptor.</param>
        /// <returns>A security token.</returns>
        /// <exception cref="ArgumentNullException">Thrown if 'tokenDescriptor' is null.</exception>
        public override SecurityToken CreateToken(SecurityTokenDescriptor tokenDescriptor)
        {
            if (null == tokenDescriptor)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("tokenDescriptor");
            }

            if (this.Configuration == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ID4272)));
            }

            ClaimsPrincipal principal = new ClaimsPrincipal(tokenDescriptor.Subject);

            if (this.Configuration.SaveBootstrapContext)
            {
                SecurityTokenHandlerCollection bootstrapTokenCollection = CreateBootstrapTokenHandlerCollection();
                if (!bootstrapTokenCollection.CanWriteToken(tokenDescriptor.Token))
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ID4010, tokenDescriptor.Token.GetType().ToString())));
                }

                (principal.Identities as ReadOnlyCollection <ClaimsIdentity>)[0].BootstrapContext = new BootstrapContext(tokenDescriptor.Token, bootstrapTokenCollection[tokenDescriptor.Token.GetType()]);
            }

            DateTime validFrom = (tokenDescriptor.Lifetime.Created.HasValue) ? (DateTime)tokenDescriptor.Lifetime.Created : DateTime.UtcNow;
            DateTime validTo   = (tokenDescriptor.Lifetime.Expires.HasValue) ? (DateTime)tokenDescriptor.Lifetime.Expires : DateTime.UtcNow + SessionSecurityTokenHandler.DefaultTokenLifetime;

            return(new SessionSecurityToken(principal, null, validFrom, validTo));
        }
Beispiel #2
0
 /// <summary>
 /// Checks if one of the wrapped SecurityTokenHandlers or the base WSSecurityTokenSerializer
 /// can write the given security token.
 /// </summary>
 /// <param name="token">SecurityToken instance.</param>
 /// <returns>'True' if the serializer can write the given security token.</returns>
 protected override bool CanWriteTokenCore(SecurityToken token)
 {
     return(_securityTokenHandlers.CanWriteToken(token));
 }