public async Task <DateTime?> DoCheckInOrOutAsync(string userName, string password) { var regexSuccessCheckIn = new Regex(@"success:\s*true"); var checkInOrOutUri = new Uri(baseUri, "SaveTimmingEvent"); var httpClient = new HttpClient(); var checkinDateTime = await RemoteDatetimeAsync(); try { var content = new Windows.Web.Http.HttpFormUrlEncodedContent(BuildHttpFormContentForCheckInOrOut(userName, password)); var response = await httpClient.PostAsync(checkInOrOutUri, content); var responseContent = await response.Content.ReadAsStringAsync(); var responseJson = FixJson(responseContent); var responseType = new { success = false, msg = new { type = 0, msg = "" } }; var responseObject = JsonConvert.DeserializeAnonymousType(responseJson, responseType); if (IsCheckSaved((int)responseObject.msg.type) && responseObject.success) { return(checkinDateTime); } else { return(null); } } catch { } return(null); }
/// <summary> /// Attemps to login to the server /// </summary> /// <param name="username">The Username</param> /// <param name="password">The Password</param> /// <returns> /// true if the app was able to login /// </returns> public async Task<LoginStatus> LoginAsync(string username, string password) { UriBuilder builder; try { builder = new UriBuilder(new Uri(settings.ServerUrl)); builder.Path = "/Account/Login"; } catch(UriFormatException) { return LoginStatus.InvalidServerUrl; } HttpBaseProtocolFilter httpFilter = new HttpBaseProtocolFilter(); using (var client = new HttpClient(httpFilter)) { var content = new Windows.Web.Http.HttpFormUrlEncodedContent(new KeyValuePair<String, String>[] { new KeyValuePair<string, string>("Email", username), new KeyValuePair<string, string>("Password", password), new KeyValuePair<string, string>("RememberMe", "true") }); HttpResponseMessage result = null; try { result = await client.PostAsync(builder.Uri, content); } catch (COMException ce) { if(ce.HResult == -2147012867) { return LoginStatus.UnableToConnectToServer; } return LoginStatus.Error; } if (result.StatusCode == HttpStatusCode.Ok) { var cookies = httpFilter.CookieManager.GetCookies(new Uri($"{builder.Scheme}://{builder.Host}:{builder.Port}")); var authz = cookies.FirstOrDefault(i => String.Compare(i.Name, "Authz") == 0); if(authz != null) { this.authz = authz; return LoginStatus.Success; } else { return LoginStatus.ErrorAuthenticating; } } } return LoginStatus.Unknown; }
private async Task <OAuthToken> GetAuthenticationTokenAsync(String packageSid, String clientSecret) { var content = new Windows.Web.Http.HttpFormUrlEncodedContent( new Dictionary <String, String> { { "grant_type", "client_credentials" }, { "client_id", packageSid }, { "client_secret", clientSecret }, { "scope", "notify.windows.com" } }); using (var response = await new HttpClient() .PostAsync(new Uri("https://login.live.com/accesstoken.srf"), content) .AsTask().ConfigureAwait(false)) { return((OAuthToken) new DataContractJsonSerializer(typeof(OAuthToken)) .ReadObject((await response.Content.ReadAsInputStreamAsync() .AsTask().ConfigureAwait(false)).AsStreamForRead())); } }
public async Task <bool> SendCommandAsync(string deviceName, string commandName, KeyValuePair <string, string> methodparams) { bool result = false; if (App.AppData.AuthResult == null) { throw new ArgumentNullException("AuthResult"); } dynamic results = false; Windows.Web.Http.HttpFormUrlEncodedContent content = null; if (methodparams.Key != null && !string.IsNullOrEmpty(methodparams.Key)) { content = new HttpFormUrlEncodedContent(new[] { methodparams }); } var response = await this.httpClient.PostAsync(new Uri(App.AucoveiRestBaseAddress + $"/api/v1/devices/{deviceName}/commands/{commandName}"), content); if (response.IsSuccessStatusCode) { //results = await response.Content.ReadAsStringAsync(); result = true; } else { if (response.StatusCode == HttpStatusCode.Unauthorized) { throw new UnauthorizedAccessException("Sorry, you don't have access to the Service. Please sign-in again."); } else { throw new UnauthorizedAccessException("Sorry, an error occurred accessing the service. Please try again."); } } return(result); }
public async Task<DateTime?> DoCheckInOrOutAsync(string userName, string password) { var regexSuccessCheckIn = new Regex(@"success:\s*true"); var checkInOrOutUri = new Uri(baseUri, "SaveTimmingEvent"); var httpClient = new HttpClient(); var checkinDateTime = await RemoteDatetimeAsync(); try { var content = new Windows.Web.Http.HttpFormUrlEncodedContent(BuildHttpFormContentForCheckInOrOut(userName, password)); var response = await httpClient.PostAsync(checkInOrOutUri, content); var responseContent = await response.Content.ReadAsStringAsync(); var responseJson = FixJson(responseContent); var responseType = new { success = false, msg = new { type = 0, msg = "" } }; var responseObject = JsonConvert.DeserializeAnonymousType(responseJson, responseType); if (IsCheckSaved((int)responseObject.msg.type) && responseObject.success) { return checkinDateTime; } else { return null; } } catch { } return null; }