public async void attemptRegister(String email, String password, String firstname, String lastname) { ServerQuery q = new ServerQuery (); ServerResponse rep = null; emailField.IsEnabled = false; passwordField.IsEnabled = false; firstnameField.IsEnabled = false; lastnameField.IsEnabled = false; submitButton.IsEnabled = false; try { Dictionary<String, String> postParams = new Dictionary<String, String>(); postParams.Add("password", password); postParams.Add("email", email); postParams.Add("firstname", firstname); postParams.Add("lastname", lastname); rep = await q.Post (ServerQuery.SERVER_URL + "/auth/subscribe", null, postParams, AuthType.APP); } catch(WebException e) { Console.WriteLine (e.ToString()); await DisplayAlert ("Erreur", "Impossible de joindre le serveur, veuillez vĂ©rifier votre connexion.", "OK"); } if (rep != null) { Console.WriteLine ("Reponse : " + rep.content.ToString()); if (rep.content.ContainsKey("code")) { int code = rep.content["code"]; if (code == 0) { await DisplayAlert ("Bienvenue", "Vous ĂȘtes maitenant inscrit", "OK"); await this.Navigation.PopAsync (); } else { emailField.IsEnabled = true; passwordField.IsEnabled = true; firstnameField.IsEnabled = true; lastnameField.IsEnabled = true; submitButton.IsEnabled = true; if(rep.content.ContainsKey("message")) await DisplayAlert ("Erreur", "Erreur durant l'inscription : " + rep.content["message"], "OK"); else await DisplayAlert ("Erreur", "Une erreur est survenue", "OK"); } } } }
/// <summary> /// Authenticate a User with the specified username and password. /// Returns TRUE if successful or FALSE otherwise. /// </summary> /// <param name="username">Username.</param> /// <param name="password">Password.</param> public async Task<Boolean> Authenticate(string username, string password) { Dictionary<String, String> body = new Dictionary<String, String> (); ServerQuery server = new ServerQuery (); body.Add ("client_id", AppAuth.CLIENT_ID); body.Add ("client_secret", AppAuth.CLIENT_SECRET); body.Add ("grant_type", "password"); body.Add ("username", username); body.Add ("password", password); ServerResponse response = await server.Post (ServerQuery.SERVER_URL + "/oauth/token", null, body, AuthType.NONE); if (response != null && response.isOk()) { if (response.content.ContainsKey ("access_token") && response.content.ContainsKey ("refresh_token")) { AccessToken = (String) response.content ["access_token"]; RefreshToken = (String) response.content ["refresh_token"]; return true; } } return false; }
/// <summary> /// Attempt to refresh the Application's access token /// If the refresh token is reject then a request for a fresh token will be sent /// </summary> public async Task<ServerResponse> Refresh() { Dictionary<String, String> body = new Dictionary<String, String> (); ServerQuery server = new ServerQuery (); body.Add ("client_id", CLIENT_ID); body.Add ("client_secret", CLIENT_SECRET); if (RefreshToken != null) { body.Add ("grant_type", "refresh_token"); body.Add ("refresh_token", RefreshToken); } else { body.Add ("grant_type", "client_credentials"); } ServerResponse response = await server.Post (ServerQuery.SERVER_URL + "/oauth/token", null, body, AuthType.NONE); if (response != null && response.isBad()) { // refresh token rejected AccessToken = null; RefreshToken = null; return await Refresh (); } AccessToken = (String) response.content ["access_token"]; RefreshToken = (String) response.content ["refresh_token"]; return response; }