Пример #1
0
        public async Task <bool> LoginSession(string username, string password, string clientos)
        {
            var param    = new AccessTokenRM(username, password, clientos);
            var response = await _sgService
                           .LoginSession(Priority.UserInitiated, param)
                           .ConfigureAwait(false);

            if (response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                var responseDto = new AccessTokenDto();
                //responseDto = await response.Content.ReadAsAsync<AccessTokenDto>(new[] { new JsonMediaTypeFormatter() });
                var responseString = response.Content.ReadAsStringAsync().Result;
                responseDto = JsonConvert.DeserializeObject <AccessTokenDto>(responseString);
                var appinfoDto = JsonConvert.DeserializeObject <AppInfo>(responseDto.appinfo);

                Globals.LoginStatus    = LoginStatusType.Success;
                Globals.LoginUsername  = responseDto.username;
                Globals.AccessToken    = responseDto.access_token;
                Globals.RefreshToken   = responseDto.refresh_token;
                Globals.AppInformation = appinfoDto;
                this.accessToken       = responseDto.access_token;
                this.tokenType         = responseDto.token_type;
                this.authToken         = this.tokenType + " " + this.accessToken;

                return(true);
            }
            else if (response.StatusCode == System.Net.HttpStatusCode.BadRequest)
            {
                var responseErrorDto = new AccessTokenErrorDto();
                //responseErrorDto = await response.Content.ReadAsAsync<AccessTokenErrorDto>(new[] { new JsonMediaTypeFormatter() });
                //var responseString = response.Content.ReadAsStringAsync().Result;
                //responseErrorDto = JsonConvert.DeserializeObject<AccessTokenErrorDto>(responseString);

                var responseString = "";
                using (var responseStream = await response.Content.ReadAsStreamAsync())
                {
                    responseString = new StreamReader(responseStream).ReadToEnd();
                }
                responseErrorDto = JsonConvert.DeserializeObject <AccessTokenErrorDto>(responseString);

                Globals.LoginStatus      = LoginStatusType.LoginError;
                Globals.ErrorTitle       = responseErrorDto.error;
                Globals.ErrorDescription = responseErrorDto.error_description;

                return(true);
            }

            return(false);
        }
Пример #2
0
        public async Task <HttpResponseMessage> LoginSession(Priority priority, AccessTokenRM param)
        {
            HttpResponseMessage        response = null;
            Task <HttpResponseMessage> task;

            string contentType = "application/x-www-form-urlencoded";

            switch (priority)
            {
            case Priority.Background:
                task = _apiService.Background.LoginSession(param.toDictionary(), contentType);
                break;

            case Priority.UserInitiated:
                task = _apiService.UserInitiated.LoginSession(param.toDictionary(), contentType);
                break;

            case Priority.Speculative:
                task = _apiService.Speculative.LoginSession(param.toDictionary(), contentType);
                break;

            default:
                task = _apiService.UserInitiated.LoginSession(param.toDictionary(), contentType);
                break;
            }

            if (CrossConnectivity.Current.IsConnected)
            {
                response = await Policy
                           .Handle <Exception>()
                           .WaitAndRetryAsync
                           (
                    retryCount: 3,
                    sleepDurationProvider: retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))
                           )
                           .ExecuteAsync(async() => await task);
            }

            return(response);
        }