public bool Login(string username, string password) { using (HttpClient httpClient = new HttpClient()) { httpClient.BaseAddress = new Uri(WebApiBaseUrl); StringContent content = null; Dictionary <string, string> loginResult = null; var loginData = new { username = username, password = password }; string dataJson = JsonConvert.SerializeObject(loginData); content = new StringContent(dataJson, Encoding.UTF8, "application/json"); var response = httpClient.PostAsync("Auth/login", content).Result; if (response.IsSuccessStatusCode) { loginResult = JsonConvert.DeserializeObject <Dictionary <string, string> >(response.Content.ReadAsStringAsync().Result); if (loginResult != null && loginResult.ContainsKey("token")) { _authToken = loginResult["token"]; OnStatusOK?.Invoke(this, new WSStatus() { StatusType = WSStatusType.LoginStatus, Status = AuthToken }); return(true); } else { OnStatusError?.Invoke(this, new WSStatus() { StatusType = WSStatusType.LoginStatus, Status = "No authorized token was found in response." }); return(false); } } else { string error = response.Content != null?response.Content.ReadAsStringAsync().Result : ""; OnStatusError?.Invoke(this, new WSStatus() { StatusType = WSStatusType.LoginStatus, Status = $"StatusCode:{response.StatusCode.ToString()}, Error:{error}" }); return(false); } } }
public void RaiseKeepAliveEvent(bool bError, string msg) { if (!bError) { OnStatusOK?.Invoke(this, new WSStatus() { StatusType = WSStatusType.SocketStatus, Status = msg }); } else { OnStatusError?.Invoke(this, new WSStatus() { StatusType = WSStatusType.SocketStatus, Status = msg }); } }
public void SignIn() { using (HttpClient httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AuthToken); httpClient.BaseAddress = new Uri(WebApiBaseUrl); Dictionary <string, string> signInResult = null; string url = $"Notification/signin?id={_wsConnection.ConnectionId}"; var response = httpClient.GetAsync(url).Result; if (response.IsSuccessStatusCode) { signInResult = JsonConvert.DeserializeObject <Dictionary <string, string> >(response.Content.ReadAsStringAsync().Result); int keepaliveInterval; if (signInResult != null && signInResult.ContainsKey("keepalive")) { if (int.TryParse(signInResult["keepalive"], out keepaliveInterval)) { OnStatusOK?.Invoke(this, new WSStatus() { StatusType = WSStatusType.SignInStatus, Status = $"KeepAlive interval is {keepaliveInterval * 0.7} seconds." }); } else { keepaliveInterval = 5; OnStatusError?.Invoke(this, new WSStatus() { StatusType = WSStatusType.SignInStatus, Status = "The format of KeepAlive interval is invalid. Using defaut value instead." }); } } else { keepaliveInterval = 5; OnStatusError?.Invoke(this, new WSStatus() { StatusType = WSStatusType.SignInStatus, Status = "No keepalive interval was found in response. Using defaut value instead." }); } OnStatusOK?.Invoke(this, new WSStatus() { StatusType = WSStatusType.SignInStatus, Status = "Signed in succeed." }); double client_keepaliveInterval = keepaliveInterval * KeepAliveRatio; _obsKeepAlive = Observable.Interval(TimeSpan.FromSeconds(client_keepaliveInterval)); hKeepAliveRef = _obsKeepAlive.Subscribe(_kaWatchDog); } else { string error = response.Content != null?response.Content.ReadAsStringAsync().Result : ""; OnStatusError?.Invoke(this, new WSStatus() { StatusType = WSStatusType.SignInStatus, Status = $"StatusCode:{response.StatusCode.ToString()}, Error:{error}" }); } } }