示例#1
0
        public bool Check(string tokenStr, string salt)
        {
            // 1、格式是否合法
            string[] parts = tokenStr.Split(".");
            if (parts.Length != 3)
            {
                return(false);
            }

            // 2、签名是否对应
            string signature = EncodingExt.FromBase64Url(parts[2], null);

            if (!ComputeHash(parts[0], parts[1], salt).EqualsNoCase(signature))
            {
                return(false);
            }

            // 3、头部是否合法
            var header = Decode(tokenStr).Header;

            if (!header.Alg.EqualsNoCase(TokenConfig.Alg) || !header.Type.EqualsNoCase(TokenConfig.Type))
            {
                return(false);
            }

            return(true);
        }
示例#2
0
        public void FromBase64Url()
        {
            string str    = "ZFpXRnlUQTVZVGcwZERNME5_Ris-PT0";
            string actual = EncodingExt.FromBase64Url(str, null);
            string expect = "dZWFyTA5YTg0dDM0NF+>==";

            Assert.AreEqual(expect, actual);
        }
示例#3
0
        public JwtToken Decode(string tokenStr)
        {
            string[] parts = tokenStr.Split(".");

            var header  = SerializeExt.JsonTo <JwtHeader>(EncodingExt.FromBase64Url(parts[0], null));
            var payload = SerializeExt.JsonTo <JwtPayload>(EncodingExt.FromBase64Url(parts[1], null));

            return(new JwtToken
            {
                Header = header,
                Payload = payload
            });
        }