private async void WebView_Navigating(object sender, WebNavigatedEventArgs e) { gridLoading.IsVisible = false; //if (e.Url.StartsWith(Configuration.AppConfig.API_IP, StringComparison.Ordinal)) { try { string queryString = e.Url.Replace(CallBackUrl, ""); Dictionary <string, string> keyValues = ParseQueryString(queryString); if (keyValues.ContainsKey("code") == false) { return; } string code = keyValues["code"]; using (HttpClient client = new HttpClient()) { HttpResponseMessage response = await client.GetAsync($"https://oauth.zaloapp.com/v3/access_token?app_id={ZALOAPPID}&app_secret={AppScret}&code={code}"); if (response.IsSuccessStatusCode) { HttpContent auContent = response.Content; string authBody = await auContent.ReadAsStringAsync(); ZaloAuthReponse zaloAuthReponse = JsonConvert.DeserializeObject <ZaloAuthReponse>(authBody); var profileResponse = await client.GetAsync($"https://graph.zalo.me/v2.0/me?access_token={zaloAuthReponse.access_token}&fields=id,birthday,name,gender,picture"); var profileBody = await profileResponse.Content.ReadAsStringAsync(); ZaloUser zaloUser = JsonConvert.DeserializeObject <ZaloUser>(profileBody); MessagingCenter.Send <ZaloLogin, ZaloUser>(this, "ZaloLoginCallback", zaloUser); } else { await DisplayAlert("", "Lỗi hệ thống, vui lòng thử lại.", "Đóng"); } } } catch (Exception ex) { await DisplayAlert("", "Lỗi hệ thống, vui lòng thử lại." + Environment.NewLine + ex.Message, "Đóng"); } } }
private async void WebView_NavigatingSocialLinked(object sender, WebNavigatedEventArgs e) { //gridLoading.IsVisible = false; if (e.Url.StartsWith("https://bsdinsight.net", StringComparison.Ordinal)) { try { string queryString = e.Url.Replace(CallBackUrl, ""); Dictionary <string, string> keyValues = ParseQueryString(queryString); if (keyValues.ContainsKey("code") == false) { return; } string code = keyValues["code"]; using (HttpClient client = new HttpClient()) { HttpResponseMessage response = await client.GetAsync($"https://oauth.zaloapp.com/v3/access_token?app_id={ZALOAPPID}&app_secret={AppScret}&code={code}"); if (response.IsSuccessStatusCode) { HttpContent auContent = response.Content; string authBody = await auContent.ReadAsStringAsync(); ZaloAuthReponse zaloAuthReponse = JsonConvert.DeserializeObject <ZaloAuthReponse>(authBody); var profileResponse = await client.GetAsync($"https://graph.zalo.me/v2.0/me?access_token={zaloAuthReponse.access_token}&fields=id,birthday,name,gender,picture"); var profileBody = await profileResponse.Content.ReadAsStringAsync(); ZaloUser zaloUser = JsonConvert.DeserializeObject <ZaloUser>(profileBody); var zlUser = new User() { ZaloId = long.Parse(zaloUser.id), FullName = zaloUser.name, Sex = zaloUser.gender == "male" ? (short)0 : zaloUser.gender == "female" ? (short)1 : (short)-1, Birthday = DateTime.ParseExact(zaloUser.birthday, "dd/MM/yyyy", null), AvatarUrl = zaloUser.picture.data.url }; try { ApiResponse apiResponse = await ApiHelper.Put("api/auth/sociallinked", zlUser, true); if (apiResponse.IsSuccess) { var user = JsonConvert.DeserializeObject <User>(apiResponse.Content.ToString()); UserLogged.SaveProfile(user); viewModel.IsZaloLoginVisible = false; await Application.Current.MainPage.DisplayAlert("", Language.lien_ket_zalo_thanh_cong, Language.dong); MessagingCenter.Send <SocialLinkedPage, bool>(this, "UpdateSocialLinked", true); } else { throw new Exception(apiResponse.Message); } } catch (Exception ex) { await Application.Current.MainPage.DisplayAlert("", ex.Message, Language.dong); await Navigation.PopAsync(); } } else { await DisplayAlert("", Language.loi_he_thong_vui_long_thu_lai, Language.dong); await this.Navigation.PopToRootAsync(); } } } catch (Exception ex) { await DisplayAlert("", Language.loi_he_thong_vui_long_thu_lai, Language.dong); await this.Navigation.PopToRootAsync(); } } }
public async void SocialLogin <T>(T user) where T : class { object data = null; FacebookUser facebookUser = null; GoogleUser googleUser = null; ZaloUser zaloUser = null; string email = null; if (user.GetType() == typeof(FacebookUser)) { facebookUser = user as FacebookUser; data = new { FullName = facebookUser.FullName, Email = facebookUser.Email, FacebookId = facebookUser.Id, PictureUrl = facebookUser.Picture }; if (!string.IsNullOrEmpty(facebookUser.Email)) { email = facebookUser.Email; } if (email == null) { await Shell.Current.DisplayAlert("", "Vui lòng cung cấp địa chỉ Email để đăng nhập ", "Đóng"); //customTabbed_Login.ShowLoading(false); return; } } else if (user.GetType() == typeof(GoogleUser)) { googleUser = user as GoogleUser; data = new { FullName = googleUser.Name, Email = googleUser.Email, GoogleId = googleUser.Id, PictureUrl = googleUser.Picture }; if (!string.IsNullOrEmpty(googleUser.Email)) { email = googleUser.Email; } if (email == null) { await Shell.Current.DisplayAlert("", "Vui lòng cung cấp địa chỉ Email để đăng nhập ", "Đóng"); //customTabbed_Login.ShowLoading(false); return; } } if (user.GetType() == typeof(ZaloUser)) { zaloUser = user as ZaloUser; data = new { FullName = zaloUser.name, ZaloId = zaloUser.id, PictureUrl = zaloUser?.picture?.data?.url ?? "", Birthday = zaloUser.birthday, Sex = zaloUser.gender }; } ApiResponse loginResponse = await ApiHelper.Post("api/auth/facebooklogin", data); if (loginResponse.IsSuccess) { AuthResponse authResponse = JsonConvert.DeserializeObject <AuthResponse>(loginResponse.Content.ToString()); UserLogged.UserName = authResponse.Email; UserLogged.Password = UserLogged.UserName; if (googleUser != null) { UserLogged.GoogleId = googleUser.Id; } else if (facebookUser != null) { UserLogged.FacebookId = facebookUser.Id; } if (zaloUser != null) { UserLogged.ZaloId = zaloUser.id; } UserLogged.SaveLogin(authResponse); Application.Current.MainPage = new AppShell(); } else { string ErrorMessage = loginResponse.GetFirstErrorMessage(); if (string.IsNullOrEmpty(ErrorMessage)) { ErrorMessage = "Lỗi đăng nhập vui lòng thử lại"; } await Shell.Current.DisplayAlert("", ErrorMessage, "Đóng"); } }