/// <summary> /// Generates an instance of JWT as a JSON Compact serialized format string /// </summary> /// <param name="subject">The source of the claim(s) for this token</param> /// <param name="options">Options to generate the JWT</param> /// <returns>The JWT as a signed (if required) encoded string.</returns> public string Generate( ClaimsIdentity subject, JwtGenerationOptions options) { options.NotNull(nameof(options)); var now = _dateTimeNowFactory(); var tokenHandler = _securityTokenHandlerFactory(); var securityToken = tokenHandler .CreateToken( issuer: "default", subject: subject, notBefore: now, expires: GetExpiresAt(now, options), signingCredentials: GetSigningCredentials(options.SigningCredentials)); return tokenHandler.WriteToken(securityToken); }
private DateTime? GetExpiresAt( DateTime now, JwtGenerationOptions options) { return now.AddMinutes(options.TokenLifetimeInMinutes); }
/// <summary> /// Generates an instance of JWT as a JSON Compact serialized format string /// </summary> /// <param name="subject">The source of the claim(s) for this token</param> /// <param name="options">Options to generate the JWT</param> /// <returns>The JWT as a signed (if required) encoded string.</returns> public string Generate( ClaimsIdentity subject, JwtGenerationOptions options) { return new JwtGenerator().Generate(subject, options); }