public void JWT_Test_05_CreateToken() { using (JwtFactory Factory = new JwtFactory(Encoding.ASCII.GetBytes("secret"))) { DateTime Expires = DateTime.Today.ToUniversalTime().AddDays(2); string TokenStr = Factory.Create( new KeyValuePair <string, object>("sub", "test user"), new KeyValuePair <string, object>("exp", Expires)); JwtToken Token = new JwtToken(TokenStr); Assert.AreEqual("JWT", Token.Type); Assert.IsTrue(Token.Algorithm is HmacSha256); Assert.AreEqual(true, Token.TryGetClaim("sub", out object Subject)); Assert.AreEqual("test user", Subject); Assert.AreEqual("test user", Token.Subject); Assert.AreEqual(Expires, Token.Expiration); Assert.AreEqual(true, Factory.IsValid(Token)); } }
public void JWT_Test_04_Validate_InvalidToken() { JwtToken Token = new JwtToken("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ"); Assert.AreEqual("JWT", Token.Type); Assert.IsTrue(Token.Algorithm is HmacSha256); Assert.AreEqual(true, Token.TryGetClaim("sub", out object Subject)); Assert.AreEqual("1234567890", Subject); Assert.AreEqual("1234567890", Token.Subject); Assert.AreEqual(true, Token.TryGetClaim("name", out object Name)); Assert.AreEqual("John Doe", Name); Assert.AreEqual(true, Token.TryGetClaim("admin", out object Admin)); Assert.AreEqual(true, Admin); using (JwtFactory Factory = new JwtFactory(Encoding.ASCII.GetBytes("wrong secret"))) { Assert.AreEqual(false, Factory.IsValid(Token)); } }
public void JWT_Test_05_CreateToken() { using (JwtFactory Factory = new JwtFactory(Encoding.ASCII.GetBytes("secret"))) { DateTime Expires = new DateTime(2018, 1, 1, 0, 0, 0, DateTimeKind.Utc); string TokenStr = Factory.Create( new KeyValuePair <string, object>("sub", "test user"), new KeyValuePair <string, object>("exp", Expires)); JwtToken Token = new JwtToken(TokenStr); Assert.AreEqual("JWT", Token.Type); Assert.AreEqual(JwtAlgorithm.HS256, Token.Algorithm); Assert.AreEqual(true, Token.TryGetClaim("sub", out object Subject)); Assert.AreEqual("test user", Subject); Assert.AreEqual("test user", Token.Subject); Assert.AreEqual(Expires, Token.Expiration); Assert.AreEqual(true, Factory.IsValid(Token)); } }