Exemple #1
0
        private void Parse(string accessToken)
        {
            string[] parts         = accessToken.Split('.');
            string   payload       = Encoding.UTF8.GetString(Base64Decode(parts[1]));
            JObject  payloadObject = JObject.Parse(payload);

            Sid   = EsiaHelpers.PropertyValueIfExists("urn:esia:sid", payloadObject);
            SbjId = EsiaHelpers.PropertyValueIfExists("urn:esia:sbj_id", payloadObject);

            double seconds;
            string value = EsiaHelpers.PropertyValueIfExists("exp", payloadObject);

            if (value != null && Double.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out seconds))
            {
                EndDate = EsiaHelpers.DateFromUnixSeconds(seconds);
            }

            value = EsiaHelpers.PropertyValueIfExists("nbf", payloadObject);

            if (value != null && Double.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out seconds))
            {
                BeginDate = EsiaHelpers.DateFromUnixSeconds(seconds);
            }

            value = EsiaHelpers.PropertyValueIfExists("iat", payloadObject);

            if (value != null && Double.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out seconds))
            {
                CreateDate = EsiaHelpers.DateFromUnixSeconds(seconds);
            }
        }
Exemple #2
0
        /// <summary>
        /// Initialize a new instance with access token parameters
        /// </summary>
        /// <param name="accessToken">Access token</param>
        /// <param name="refreshToken">Refresh token</param>
        /// <param name="expiresIn">Expires in</param>
        /// <param name="payload">Payload object to parse</param>
        public EsiaToken(string accessToken, string refreshToken, string expiresIn, JObject payload) : this(accessToken)
        {
            RefreshToken = refreshToken;

            int expiresValue;

            if (Int32.TryParse(expiresIn, NumberStyles.Integer, CultureInfo.InvariantCulture, out expiresValue))
            {
                ExpiresIn = TimeSpan.FromSeconds(expiresValue);
            }

            if (payload != null)
            {
                Sid   = EsiaHelpers.PropertyValueIfExists("urn:esia:sid", payload);
                SbjId = EsiaHelpers.PropertyValueIfExists("urn:esia:sbj_id", payload);

                double seconds;
                string value = EsiaHelpers.PropertyValueIfExists("exp", payload);

                if (value != null && Double.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out seconds))
                {
                    EndDate = EsiaHelpers.DateFromUnixSeconds(seconds);
                }

                value = EsiaHelpers.PropertyValueIfExists("nbf", payload);

                if (value != null && Double.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out seconds))
                {
                    BeginDate = EsiaHelpers.DateFromUnixSeconds(seconds);
                }

                value = EsiaHelpers.PropertyValueIfExists("iat", payload);

                if (value != null && Double.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out seconds))
                {
                    CreateDate = EsiaHelpers.DateFromUnixSeconds(seconds);
                }
            }
        }