private void SetupUserInterface() { var board = UIStoryboard.FromName("Main", null); var viewController = board.InstantiateInitialViewController(); ViewController.AddChildViewController(viewController); ViewController.View.Add(viewController.View); }
protected override void OnElementChanged(VisualElementChangedEventArgs e) { base.OnElementChanged(e); var auth = new OAuth2Authenticator( clientId: Constants.AppId, clientSecret: Constants.AppSecretId, scope: Constants.ExtendedPermissions, authorizeUrl: new Uri(Constants.AuthorizeUrl), redirectUrl: new Uri(Constants.RedirectUrl), accessTokenUrl: new Uri(Constants.TokenUrl)); auth.AllowCancel = false; // If authorization succeeds or is canceled, .Completed will be fired. auth.Completed += async(s, eargs) => { if (!eargs.IsAuthenticated) { App.IsLoggedIn = false; ((App)App.Current).PresentLogin("Canceled!"); return; } var token = eargs.Account.Properties["access_token"]; await App.Current.MainPage.Navigation.PopModalAsync(); App.IsLoggedIn = true; ((App)App.Current).PresentMainPage(); }; UIViewController vc = auth.GetUI(); ViewController.AddChildViewController(vc); ViewController.View.Add(vc.View); vc.ChildViewControllers[0].NavigationItem.LeftBarButtonItem = new UIBarButtonItem( UIBarButtonSystemItem.Cancel, async(o, eargs) => await App.Current.MainPage.Navigation.PopModalAsync() ); }
void LoginToFacebook(bool allowCancel) { var loginPage = Element as FBLoginPage; string providername = loginPage.ProviderName; OAuth2Authenticator auth = null; switch (providername) { case "Google": { auth = new OAuth2Authenticator( // For Google login, for configure refer https://code.msdn.microsoft.com/Register-Identity-Provider-41955544 App.ClientId, App.ClientSecret, // Below values do not need changing "https://www.googleapis.com/auth/userinfo.email", new Uri(App.url2), new Uri(App.url3), // Set this property to the location the user will be redirected too after successfully authenticating new Uri(App.url4) ); break; } case "FaceBook": { auth = new OAuth2Authenticator( clientId: App.AppId, scope: App.ExtendedPermissions, authorizeUrl: new Uri(App.AuthorizeUrl), redirectUrl: new Uri(App.RedirectUrl)); break; } } auth.AllowCancel = allowCancel; // If authorization succeeds or is canceled, .Completed will be fired. auth.Completed += async(s, eargs) => { if (!eargs.IsAuthenticated) { return; } else { //var token = eargs.Account.Properties["access_token"]; if (providername.Equals("FaceBook")) { // Now that we're logged in, make a OAuth2 request to get the user's info. var request = new OAuth2Request("GET", new Uri("https://graph.facebook.com/me?fields=id,name,picture,email"), null, eargs.Account); var result = await request.GetResponseAsync(); string resultText = result.GetResponseText(); var obj = JsonValue.Parse(resultText); // Console.WriteLine(token + " -=- " + resultText); App.FacebookId = obj["id"]; App.FacebookName = obj["name"]; App.EmailAddress = obj["email"]; App.ProfilePic = obj["picture"]["data"]["url"]; // //saveset(obj["id"], obj["name"]); // Get Shared User Defaults var plist1 = NSUserDefaults.StandardUserDefaults; // Save value plist1.SetString(obj["id"], "PrefId"); plist1.SetString(obj["name"], "PrefName"); // Sync changes to database plist1.Synchronize(); } else { string url1 = "https://www.googleapis.com/oauth2/v2/userinfo"; var request = new OAuth2Request("GET", new Uri(url1), null, eargs.Account); var response = await request.GetResponseAsync(); if (response != null) { HttpClient client1 = new HttpClient(); client1.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); client1.MaxResponseContentBufferSize = 256000; //var data = client.SendAsync(request).Result; string url = response.ResponseUri.ToString(); var data = client1.GetStringAsync(url).Result; //var obj = JsonValue.Parse(data); var obj = JsonConvert.DeserializeObject <UserM>(data); App.FacebookId = obj.Id; App.FacebookName = obj.Name; App.EmailAddress = obj.Email; App.ProfilePic = obj.Picture; // // saveset(obj["id"], obj["name"]); // Get Shared User Defaults var plist2 = NSUserDefaults.StandardUserDefaults; // Save value plist2.SetString(obj.Id, "PrefId"); plist2.SetString(obj.Name, "PrefName"); // Sync changes to database plist2.Synchronize(); } } // On Android: store the account //AccountStore.Create(Context).Save(eargs.Account, "Facebook"); //AccountStore.Create().Save(eargs.Account, "Facebook"); //Account object is securely saved on the iOS platform //Save as a new user to the database await App.UserManager.SaveTaskAsync (new Models.User { FBID = App.FacebookId, UserName = App.FacebookName, Email = App.EmailAddress, ImgLink = App.ProfilePic }, true); //retreive gcm id var plist = NSUserDefaults.StandardUserDefaults; // Get value var id = plist.StringForKey("regid"); RegisterAsync(id, new string[] { App.FacebookId + "T" }, App.FacebookId); await App.Current.MainPage.Navigation.PopModalAsync(); App.IsLoggedIn = true; ((App)App.Current).SaveProfile(); ((App)App.Current).PresentMainPage(); } }; UIViewController vc = (UIViewController)auth.GetUI(); ViewController.AddChildViewController(vc); ViewController.View.Add(vc.View); vc.ChildViewControllers[0].NavigationItem.LeftBarButtonItem = new UIBarButtonItem( UIBarButtonSystemItem.Cancel, async(o, eargs) => await App.Current.MainPage.Navigation.PopModalAsync() ); }
public override void ViewDidAppear(bool animated) { base.ViewDidAppear(animated); OAuth2Authenticator auth = null; //todo add to appsettings auth = new OAuth2Authenticator( clientId: "933191510091844", scope: "email", authorizeUrl: new Uri("https://www.facebook.com/dialog/oauth"), redirectUrl: new Uri("http://yourValidEndpoint.com/login_success.html")); // we do this to be able to control the cancel flow outself... auth.AllowCancel = false; auth.Completed += async(sender, e) => { if (!e.IsAuthenticated) { return; } else { var access = e.Account.Properties ["access_token"]; using (var handler = new ModernHttpClient.NativeMessageHandler()) { using (var client = new HttpClient(handler)) { var content = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string> ("accesstoken", access), new KeyValuePair <string, string> ("grant_type", "facebook") }); var authenticateResponse = await client.PostAsync(new Uri ("http://windows:8080/Token"), content); if (authenticateResponse.IsSuccessStatusCode) { var responseContent = await authenticateResponse.Content.ReadAsStringAsync(); var authenticationTicket = JsonConvert.DeserializeObject <AuthenticatedUser> (responseContent); if (authenticationTicket != null) { var apiAccessToken = authenticationTicket.Access_Token; Settings.ApiAccessToken = apiAccessToken; ((App)App.Current).PresentMain(); } } } } } }; UIViewController vc = auth.GetUI(); ViewController.AddChildViewController(vc); ViewController.View.Add(vc.View); // add out custom cancel button, to be able to navigate back vc.ChildViewControllers [0].NavigationItem.LeftBarButtonItem = new UIBarButtonItem( UIBarButtonSystemItem.Cancel, async(o, eargs) => await App.Current.MainPage.Navigation.PopModalAsync() ); }