Esempio n. 1
0
 public void TestInitilaize()
 {
     _webClientSampleCode=new WebClientSampleCode(BaseUrl,TokenEndpoint,
         EndpointAllStudents,EndpointStudentByIdentity,EndpointStudentByStatetestnumber);
     _token = _webClientSampleCode.GetToken();
 }
Esempio n. 2
0
        public Token RefreshToken(Token token = null)
        {
            if (token == null)
            {
                token = GetToken();
            }
            if (token == null || string.IsNullOrWhiteSpace(token.RefreshToken))
            {
                return null;
            }
            // web client
            var client = new System.Net.WebClient();


            // add headers
            client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
            string credentials = Convert.ToBase64String(
                Encoding.ASCII.GetBytes(string.Format("{0}:{1}", _credentials.ClientId, _credentials.ClientSecret)));
            client.Headers[HttpRequestHeader.Authorization] = string.Format("Basic {0}", credentials);

            // refresh token request doesn't need "username" and "password"
            var postVaules = new NameValueCollection
            {
                {"grant_type", GrantTpype.RefreshToken},
                {"refresh_token", token.RefreshToken}
            };
            try
            {
                byte[] result = client.UploadValues(TokenUrl, "POST", postVaules);
                var jsonData = Encoding.UTF8.GetString(result);
                var refreshToken = JsonConvert.DeserializeObject<Token>(jsonData);
                return refreshToken;
            }
            catch (WebException ex)
            {
                if (((HttpWebResponse)ex.Response).StatusCode == HttpStatusCode.BadRequest)
                {
                    throw new WebException("Failed to request access token");
                }
            }
            return null;
        }
Esempio n. 3
0
 public ResponseData<ChildInfo> GetStudentByStatetestnumber(Token token)
 {
     return GetStudentsByEndpoint(GetUri(_baseUrl, _endpointStudentByStatetestnumber), token);
 }
Esempio n. 4
0
 public ResponseData<ChildInfo> GetStudentByIdentity(Token token)
 {
     return GetStudentsByEndpoint(GetUri(_baseUrl, _endpointStudentByIdentity), token);
 }
Esempio n. 5
0
 public ResponseData<ChildInfo> GetAllStudents(Token token)
 {
     return GetStudentsByEndpoint(GetUri(_baseUrl, _endpointAllStudents),token);
 }
Esempio n. 6
0
        private ResponseData<ChildInfo> GetStudentsByEndpoint(Uri serviceUri, Token token)
        {
            // web client
            var client = new System.Net.WebClient();
            client.Headers["Content-type"] = "application/json";
            client.Headers[HttpRequestHeader.Authorization] = string.Format("{0} {1}", TokenType, token.AccessToken);

            try
            {
                return DownloadStudents(serviceUri, client);
            }
            catch (WebException wex)
            {
                if (((HttpWebResponse)wex.Response).StatusCode == HttpStatusCode.Unauthorized)
                {
                    // validate token failed, need to refresh token
                    token = RefreshToken(token);
                    client.Headers[HttpRequestHeader.Authorization] = string.Format("{0} {1}", TokenType, token.AccessToken);
                    return DownloadStudents(serviceUri, client); ;
                }
            }
            return null;
        }