public static void SaveLogin(AccessTokenResponse Result)
 {
     CrossSettings.Current.AddOrUpdateValue("AccessToken", Result.access_token);
     CrossSettings.Current.AddOrUpdateValue("RefreshToken", Result.refresh_token);
     CrossSettings.Current.AddOrUpdateValue("AccessTokenIssueDate", DateTime.UtcNow.ToString());
     CrossSettings.Current.AddOrUpdateValue("RefreshTokenTokenIssueDate", DateTime.UtcNow.ToString());
     CrossSettings.Current.AddOrUpdateValue("PortalUrl", ServiceApi.PortalURL);
     CrossSettings.Current.AddOrUpdateValue("User", Result.user);
 }
示例#2
0
        public async Task <AccessTokenResponse> Register(string firstName, string lastName, string username, string password, string confirmPassword)
        {
            AccessTokenResponse results = null;
            HttpClient          c       = new HttpClient(new NativeMessageHandler())
            {
                BaseAddress = new Uri(PortalURL)
            };

            var vals = new List <KeyValuePair <string, string> >();

            vals.Add(new KeyValuePair <string, string>("Email", username));
            vals.Add(new KeyValuePair <string, string>("Password", password));
            vals.Add(new KeyValuePair <string, string>("ConfirmPassword", confirmPassword));
            vals.Add(new KeyValuePair <string, string>("FirstName", firstName));
            vals.Add(new KeyValuePair <string, string>("LastName", lastName));

            if (CrossConnectivity.Current.IsConnected)
            {
                var t       = c.PostAsync(PortalURL + "/api/account/register", new FormUrlEncodedContent(vals));
                var message = await Policy
                              .Handle <WebException>().Or <IOException>()
                              .WaitAndRetryAsync(retryCount: 2, sleepDurationProvider: retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)))
                              .ExecuteAsync(async() => await t);

                string content = await message.Content.ReadAsStringAsync();

                if (!message.IsSuccessStatusCode)
                {
                    var error = JsonConvert.DeserializeObject <JObject>(content);

                    string errors = "";
                    foreach (var err in error["ModelState"])
                    {
                        errors += err.Values().FirstOrDefault() + "\n";
                    }

                    errors = errors.Trim();

                    throw new WebException(errors);
                }
                results = JsonConvert.DeserializeObject <AccessTokenResponse>(content);
                LocalStorage.SaveLogin(results);
            }
            else
            {
                throw new Exception("No internet connection.");
            }

            if (results == null)
            {
                throw new NullReferenceException("Results should not be null");
            }

            return(results);
        }
示例#3
0
        public async Task <AccessTokenResponse> RefreshToken(string refreshToken)
        {
            AccessTokenResponse results = null;
            HttpClient          c       = new HttpClient(new NativeMessageHandler())
            {
                BaseAddress = new Uri(PortalURL)
            };

            var vals = new List <KeyValuePair <string, string> >();

            vals.Add(new KeyValuePair <string, string>("refresh_token", refreshToken));
            vals.Add(new KeyValuePair <string, string>("client_id", "self"));
            vals.Add(new KeyValuePair <string, string>("grant_type", "refresh_token"));
            if (CrossConnectivity.Current.IsConnected)
            {
                var t       = c.PostAsync(PortalURL + "/token", new FormUrlEncodedContent(vals));
                var message = await Policy
                              .Handle <WebException>().Or <IOException>()
                              .WaitAndRetryAsync(retryCount: 2, sleepDurationProvider: retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)))
                              .ExecuteAsync(async() => await t);

                string content = await message.Content.ReadAsStringAsync();

                if (!message.IsSuccessStatusCode)
                {
                    var error = JsonConvert.DeserializeObject <JObject>(content);
                    throw new WebException(error["error"].ToString());
                }
                results = JsonConvert.DeserializeObject <AccessTokenResponse>(content);
                LocalStorage.SaveLogin(results);
            }
            else
            {
                throw new Exception("No internet connection.");
            }

            if (results == null)
            {
                throw new NullReferenceException("Results should not be null");
            }

            return(results);
        }