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); }
public void FromBase64Url() { string str = "ZFpXRnlUQTVZVGcwZERNME5_Ris-PT0"; string actual = EncodingExt.FromBase64Url(str, null); string expect = "dZWFyTA5YTg0dDM0NF+>=="; Assert.AreEqual(expect, actual); }
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 }); }