Esempio n. 1
0
        private async Task<ExtAccount> _GetAccountInfo(ServiceType type, AccessResponse access)
        {
            if (access == null) throw new ArgumentNullException("access");

            var config = _openIdConfigService.GetConfig(type);

            string url = config.GetUserInfoQueryUrl(access.access_token);
            WebRequest request = WebRequest.Create(new Uri(url)) as HttpWebRequest;
            if (request == null) return null;

            request.Method = WebRequestMethods.Http.Get;

            WebResponse response = await request.GetResponseAsync();
            string responseContent = _ReadStreamFromResponse(response);

            var responseObj = JsonConvert.DeserializeObject(responseContent, Type.GetType(config.UserInfoObjectType)) as IUserInfoResponse;

            if (responseObj == null) return null;

            ExtAccount extAccount = responseObj.ToAccount();
            //externalUserInfo.AccessToken = access.access_token;
            //externalUserInfo.ExpiresIn = access.expires_in;
            //externalUserInfo.LastAccess = DateTime.Now;

            return extAccount;
        }
Esempio n. 2
0
        private async Task<AccessResponse> _GetAccess(ServiceType type, string code)
        {
            if (String.IsNullOrEmpty(code)) throw new ArgumentNullException("code");

            OpenIdConfig config = _openIdConfigService.GetConfig(type);
            if (config == null) throw new InvalidOperationException("No config file was found");

            WebRequest request = WebRequest.Create(new Uri(config.GetAccessUrl())) as HttpWebRequest;

            string postData = config.GetAccessQuery(code);
            Byte[] data = Encoding.UTF8.GetBytes(postData);

            request.Method = WebRequestMethods.Http.Post;
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = data.Length;

            using (Stream stream = request.GetRequestStream())
            {
                stream.Write(data, 0, data.Length);
            }

            WebResponse response = await request.GetResponseAsync();
            string responseContent = _ReadStreamFromResponse(response);
            AccessResponse responseObj;

            try
            {
                responseObj = JsonConvert.DeserializeObject<AccessResponse>(responseContent);
            }
            catch (JsonReaderException)
            {
                var qparse = HttpUtility.ParseQueryString(responseContent);
                if (qparse["access_token"] != null)
                {
                    responseObj = new AccessResponse { access_token = qparse["access_token"] };
                }
                else
                {
                    throw;
                }
            }

            return responseObj;
        }