public async Task Connection() { var http = new HttpClient(); var idUser = new IdUser() { UserName = ULogin, Password = Password }; try { var stringInput = await http.PostAsJsonAsync("http://smartcityanimal.azurewebsites.net/api/Jwt", idUser); if (stringInput.IsSuccessStatusCode) { var content2 = await stringInput.Content.ReadAsStringAsync(); var tokenSplit = content2.Split('{', '}', ':', ','); Token.Id = tokenSplit[2].TrimEnd('\"').TrimStart('\"'); } } catch (HttpRequestException e) { Token.Id = null; } if (Token.Id == null) { GoBackHome(); } else { http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Token.Id); var response = await http.GetAsync("http://smartcityanimal.azurewebsites.net/api/Account/Admin"); if (!response.IsSuccessStatusCode) { GoBackHome(); } else { navPage.NavigateTo("UserManagement", ULogin); } } }
public async Task Connection( ) { var idUser = new IdUser() { UserName = ULogin, Password = Password }; try { var stringInput = await SingleConnection.Client.PostAsJsonAsync(SingleConnection.Client.BaseAddress + "Jwt", idUser); if (stringInput.IsSuccessStatusCode) { var content2 = await stringInput.Content.ReadAsStringAsync(); var tokenSplit = content2.Split('{', '}', ':', ','); Token.Id = tokenSplit[2].TrimEnd('\"').TrimStart('\"'); if (Token.Id == null) { navPage.NavigateTo("Login"); } else { SingleConnection.Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Token.Id); var response = await SingleConnection.Client.GetAsync(SingleConnection.Client.BaseAddress + "Account/Role/" + idUser.UserName); string roleName = await response.Content.ReadAsStringAsync(); if (ApplicationUser.GetRoleUser(roleName) != "Admin") { await dialogService.ShowMessageBox("Acces non autorisé aux utilisateurs", "Non autorisé"); } else { navPage.NavigateTo("UserManagement", ULogin); } } } else { if ((int)stringInput.StatusCode == 400 && (int)stringInput.StatusCode == 401) { await dialogService.ShowMessageBox("Le compte ou le mot de passe est incorrecte", "Erreur authentification"); } else if ((int)stringInput.StatusCode == 403) { await dialogService.ShowMessageBox("Accès non autorisé aux utilisateurs", "Non autorisé"); } else if ((int)stringInput.StatusCode > 403 && (int)stringInput.StatusCode < 409) { await dialogService.ShowMessageBox("Une erreur est intervenue veuillez réésayer", "Erreur"); } else if ((int)stringInput.StatusCode > 499 && (int)stringInput.StatusCode < 600) { await dialogService.ShowMessageBox("Impossible de se connecter au serveur", "Erreur connection"); } } } catch (HttpRequestException e) { await dialogService.ShowMessageBox("Impossible de se connecter au serveur", "Erreur"); } }