Exemple #1
0
        /// <summary>
        /// 获取token
        /// </summary>
        /// <param name="code"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public async Task <string> GetToken(string code, string state, string host)
        {
            string authCode = System.Convert.ToBase64String(Encoding.ASCII.GetBytes($"{ElemeOptions.Key}:{ElemeOptions.Secret}"));

            client.AddAuthorization("Basic", authCode);
            Dictionary <string, string> dic = new Dictionary <string, string>();

            dic.Add("grant_type", "authorization_code");
            dic.Add("code", code);
            string returnUrl = "http://" + host + "/api/elmpoi/authcallback";

            //returnUrl = HttpUtility.UrlEncode(returnUrl);
            dic.Add("redirect_uri", returnUrl);
            dic.Add("client_id", ElemeOptions.Key);
            var res = await client.PostFormAsync(ElemeOptions.GetAuthTokenUrl(), dic);

            if (res.StatusCode == System.Net.HttpStatusCode.OK)
            {
                return(res.Result);
            }
            else
            {
                throw new Exception(res.ErrorMessage);
            }
        }
Exemple #2
0
        /// <summary>
        /// 刷新token
        /// </summary>
        /// <param name="refreshToken"></param>
        /// <returns></returns>
        public async Task <string> RefreshToken(string refreshToken)
        {
            string authCode = System.Convert.ToBase64String(Encoding.ASCII.GetBytes($"{ElemeOptions.Key}:{ElemeOptions.Secret}"));

            client.AddAuthorization("Basic", authCode);
            Dictionary <string, string> dic = new Dictionary <string, string>();

            dic.Add("grant_type", "refresh_token");
            dic.Add("refresh_token", refreshToken);
            var res = await client.PostFormAsync(ElemeOptions.GetAuthTokenUrl(), dic);

            if (res.StatusCode == System.Net.HttpStatusCode.OK)
            {
                return(res.Result);
            }
            else
            {
                throw new Exception(res.ErrorMessage);
            }
        }