public void RegisterUser(RegisterUser user) { using (var client = new HttpClient()) { try { client.BaseAddress = new Uri(ConfigurationManager.AppSettings["ServerPath"]); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); dynamic myObject = new JObject(); myObject.login = user.Username; myObject.firstHash = _passwordManager.CreateFirstHash(user.Username, user.Password); myObject.firstName = user.FirstName; myObject.lastName = user.LastName; myObject.phone = user.Phone; myObject.mail = user.Mail; var result = client.PostAsync(ConfigurationManager.AppSettings["RegisterPath"], new StringContent(JsonConvert.SerializeObject(myObject).ToString(), Encoding.UTF8, "application/json")).Result; if (result.StatusCode != HttpStatusCode.OK) { throw new Exception($"Server error. Status: {result.StatusCode}. Message: {result.RequestMessage}"); } } catch (HttpRequestException e) { throw new Exception("Internal server error."); } } }
public async Task <LoginResult> LogIn(string login, string password) { using (var client = new HttpClient()) { try { client.BaseAddress = new Uri(ConfigurationManager.AppSettings["ServerPath"]); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); dynamic myObject = new JObject(); myObject.login = login; myObject.firstHash = _passwordManager.CreateFirstHash(login, password); var result = await client.PostAsync(ConfigurationManager.AppSettings["LoginPath"], new StringContent(JsonConvert.SerializeObject(myObject).ToString(), Encoding.UTF8, "application/json")); var resultString = (await result.Content.ReadAsStringAsync()).Replace("\"", ""); Guid guid; if (!Guid.TryParse(resultString, out guid)) { return(LoginResult.GetFailed()); } return(LoginResult.GetLogged(resultString)); } catch (HttpRequestException e) { LogManager.GetCurrentClassLogger().Fatal("Exception performing login at ServerPath: {0} LoginPath: {1}: {2} {3}", ConfigurationManager.AppSettings["ServerPath"], ConfigurationManager.AppSettings["LoginPath"], e.Message, e.StackTrace); return(LoginResult.GetConnectionErrorFailed()); } } }