/// <summary> /// Decodes the string into the header, payload and signature /// </summary> /// <param name="jwtEncodedString">Base64Url encoded string.</param> internal void Decode(string jwtEncodedString) { string[] strArray = jwtEncodedString.Split(new char[1] { '.' }, 4); if (strArray.Length != 3) { throw new ArgumentException(string.Format((IFormatProvider)CultureInfo.InvariantCulture, "IDX10709: '{0}' is not well formed: '{1}'. The string needs to be in compact JSON format, which is of the form: '<Base64UrlEncodedHeader>.<Base64UrlEndcodedPayload>.<OPTIONAL, Base64UrlEncodedSignature>'.", (object)nameof(jwtEncodedString), (object)jwtEncodedString)); } try { this.header = JwtHeader.Base64UrlDeserialize(strArray[0]); string typ = this.header.Typ; if (typ != null) { if (!StringComparer.Ordinal.Equals(typ, "JWT")) { if (!StringComparer.Ordinal.Equals(typ, "http://openid.net/specs/jwt/1.0")) { throw new Exception(string.Format((IFormatProvider)CultureInfo.InvariantCulture, "IDX10702: Jwt header type specified, must be '{0}' or '{1}'. Type received: '{2}'.", (object)"JWT", (object)"http://openid.net/specs/jwt/1.0", (object)typ)); } } } } catch (Exception ex) { throw new ArgumentException(string.Format((IFormatProvider)CultureInfo.InvariantCulture, "IDX10703: Unable to decode the '{0}': '{1}' as Base64url encoded string. jwtEncodedString: '{2}'.", (object)"header", (object)strArray[0], (object)jwtEncodedString), ex); } try { this.payload = JwtPayload.Base64UrlDeserialize(strArray[1]); } catch (Exception ex) { throw new ArgumentException(string.Format((IFormatProvider)CultureInfo.InvariantCulture, "IDX10703: Unable to decode the '{0}': '{1}' as Base64url encoded string. jwtEncodedString: '{2}'.", (object)"payload", (object)strArray[1], (object)jwtEncodedString), ex); } this.rawData = jwtEncodedString; this.rawHeader = strArray[0]; this.rawPayload = strArray[1]; this.rawSignature = strArray[2]; }