public static async Task <Dictionary <string, string> > GetMobileSettings() { using (var client = new HttpClient()) { var service = $"{Settings.FunctionURL}/api/MobileSettings/"; byte[] time = BitConverter.GetBytes(DateTime.UtcNow.ToBinary()); byte[] key = Guid.NewGuid().ToByteArray(); var token = Convert.ToBase64String(time.Concat(key).ToArray()); token = DependencyService.Get <ISecurityService>().Encrypt(token, Settings.Cryptography); MobileSettingsRequest request = new MobileSettingsRequest(); request.Token = token; byte[] byteData = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(request)); using (var content = new ByteArrayContent(byteData)) { content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); var httpResponse = await client.PostAsync(service, content); if (httpResponse.StatusCode == HttpStatusCode.OK) { Dictionary <string, string> result = JsonConvert.DeserializeObject <Dictionary <string, string> >(await httpResponse.Content.ReadAsStringAsync()); return(result); } } } return(null); }
public static async Task <string> RequestMobileSettings(string existingToken) { string token = string.Empty; if (string.IsNullOrEmpty(existingToken)) { byte[] time = BitConverter.GetBytes(DateTime.UtcNow.ToBinary()); byte[] key = Guid.NewGuid().ToByteArray(); token = Convert.ToBase64String(time.Concat(key).ToArray()); token = SecurityHelper.Encrypt(token, Settings.CryptographyKey); System.Console.WriteLine($"Token: {token}"); } else { token = existingToken; } MobileSettingsRequest request = new MobileSettingsRequest(); request.Token = token; using (var client = new HttpClient()) { var service = $"http://localhost:7071/api/MobileSettings/"; byte[] byteData = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(request)); using (var content = new ByteArrayContent(byteData)) { content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); var httpResponse = client.PostAsync(service, content).Result; if (httpResponse.StatusCode == HttpStatusCode.OK) { System.Console.WriteLine(httpResponse.StatusCode); return(await httpResponse.Content.ReadAsStringAsync()); } else { System.Console.WriteLine(httpResponse.StatusCode); } } } return(null); }