コード例 #1
0
        public void JsonDataRoundTrip()
        {
            var date = DateTime.UtcNow;

            var claims = new PasetoInstance
            {
                Issuer           = "http://auth.example.com",
                Subject          = "2986689",
                Audience         = "audience",
                Expiration       = new DateTime(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second, date.Kind).AddMinutes(10),
                NotBefore        = new DateTime(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second, date.Kind).AddMinutes(-10),
                IssuedAt         = new DateTime(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second, date.Kind),
                AdditionalClaims = new Dictionary <string, object>
                {
                    ["roles"] = new[] { "Admin", "User" }
                },
                Footer = new Dictionary <string, object>
                {
                    ["kid"] = "dpm0"
                },
            };

            string token       = PasetoUtility.Encrypt(_symmetricKey, claims);
            var    parsedToken = PasetoUtility.Decrypt(_symmetricKey, token, validateTimes: true);

            Assert.Equal(claims.Issuer, parsedToken.Issuer);
            Assert.Equal(claims.Subject, parsedToken.Subject);
            Assert.Equal(claims.Audience, parsedToken.Audience);
            Assert.Equal(claims.Expiration, parsedToken.Expiration);
            Assert.Equal(claims.NotBefore, parsedToken.NotBefore);
            Assert.Equal(claims.IssuedAt, parsedToken.IssuedAt);
            Assert.Equal(claims.AdditionalClaims, parsedToken.AdditionalClaims);
            Assert.Equal(claims.Footer, parsedToken.Footer);
        }