public void Encode_With_Secret_And_Payload_Should_Return_Token() { var algorithm = new HMACSHA256Algorithm(); var builder = new JwtBuilder(); const ClaimName claimKey = ClaimName.ExpirationTime; var claimValue = DateTime.UtcNow.AddHours(1) .ToString(CultureInfo.InvariantCulture); var secret = _fixture.Create <string>(); var token = builder.WithAlgorithm(algorithm) .WithSecret(secret) .AddClaim(claimValue, claimKey) .Encode(); token.Should() .NotBeNullOrEmpty("because the token should contains some data"); token.Split('.') .Should() .HaveCount(3, "because the token should consist of three parts"); }
public void Build_WithPayload() { var algorithm = new HMACSHA256Algorithm(); var builder = new JwtBuilder(); const ClaimName claimKey = ClaimName.ExpirationTime; var claimValue = DateTime.UtcNow .AddHours(1) .ToString(CultureInfo.InvariantCulture); var secret = _fixture.Create <string>(); var tokenBuilt = builder .WithAlgorithm(algorithm) .WithSecret(secret) .AddClaim(claimValue, claimKey) .Build(); tokenBuilt.Should() .NotBeEmpty("because the token should contains some data"); tokenBuilt.Split('.').Should() .HaveCount(3, "because the built token should have the three standard parts"); }
/// <summary> /// Adds well-known claim to the JWT. /// </summary> /// <param name="name">Well-known claim name</param> /// <param name="value">Claim value</param> /// <returns>Current builder instance</returns> public JwtBuilder AddClaim(ClaimName name, string value) => AddClaim(name.GetPublicClaimName(), value);
public static T GetValue <T>(this IIdentity identity, ClaimName claimName) { return(identity.GetValue <T>(claimName.GetPublicClaimName())); }
/// <summary> /// Gets the string representation of a well-known claim name enum /// </summary> public static string GetPublicClaimName(this ClaimName value) => GetDescription(value);
/// <summary> /// Adds well-known claim to the JWT. /// </summary> public static JwtBuilder AddClaim <T>(this JwtBuilder builder, ClaimName name, T value) => builder.AddClaim(name, (object)value);
/// <summary> /// Adds well-known claim to the JWT. /// </summary> public static JwtBuilder AddClaim(this JwtBuilder builder, ClaimName name, object value) => builder.AddClaim(name.GetPublicClaimName(), value);
public JwtBuilder AddClaim(ClaimName name, object value) => AddClaim(name.GetPublicClaimName(), value);