예제 #1
0
        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");
        }
예제 #2
0
        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");
        }
예제 #3
0
파일: JwtBuilder.cs 프로젝트: rakeshv1/jwt
 /// <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()));
 }
예제 #5
0
 /// <summary>
 /// Gets the string representation of a well-known claim name enum
 /// </summary>
 public static string GetPublicClaimName(this ClaimName value) =>
 GetDescription(value);
예제 #6
0
 /// <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);
예제 #7
0
 /// <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);
예제 #8
0
 public JwtBuilder AddClaim(ClaimName name, object value) => AddClaim(name.GetPublicClaimName(), value);