/// <summary> /// Closes the session. /// </summary> /// <returns></returns> public async Task CloseAsync() { if (authorizationHeader != null) { StringContent content = new StringContent(""); content.Headers.ContentType.MediaType = "application/json"; var uri = ServiceRoot.ToString().Replace("/odata", "/Logout").TrimEnd('/'); var result = await httpClient.PostAsync(uri, content); //var json = await result.Content.ReadAsStringAsync(); authorizationHeader = null; } }
/// <summary> /// Logins the specified user. /// </summary> /// <param name="credentials">The credentials.</param> /// <returns></returns> /// <exception cref="Exception">Invalid user name or password.</exception> public async Task <string> LoginAsync(ErpCredentials credentials) { StringContent content = new StringContent( string.Format("{{\"app\":\"{0}\",\"user\":\"{1}\",\"pass\":\"{2}\",\"ln\":\"{3}\"}}", credentials.ApplicationName, credentials.UserName, credentials.Password, credentials.Language)); content.Headers.ContentType.MediaType = "application/json"; var uri = ServiceRoot.ToString().Replace("/odata", "/Login").TrimEnd('/'); HttpRequestMessage msg = new HttpRequestMessage() { RequestUri = new Uri(uri), Method = HttpMethod.Post, Content = content }; msg.Headers.ExpectContinue = false; var result = await httpClient.SendAsync(msg, HttpCompletionOption.ResponseContentRead); var json = await result.Content.ReadAsStringAsync(); try { if (!result.IsSuccessStatusCode) { throw new Exception(json); } } catch (Exception ex) { throw new Exception("Invalid user name or password.", ex); } authorizationHeader = json.Split(':')[1].Trim('"', '}'); httpClient.DefaultRequestHeaders.Add("Authorization", authorizationHeader); return(json); }