public async Task LoginAsync(Xamarin.Forms.Page page)
        {
            string resourceToken = null;
            var    tcs           = new TaskCompletionSource <bool> ();

#if __IOS__
            var controller = UIKit.UIApplication.SharedApplication.KeyWindow.RootViewController;
#endif
            try {
                var auth = new Xamarin.Auth.WebRedirectAuthenticator(
                    initialUrl: new Uri(resourceTokenBrokerURL + "/.auth/login/facebook"),
                    redirectUrl: new Uri(resourceTokenBrokerURL + "/.auth/login/done"));

                auth.Completed += async(sender, e) => {
                    if (e.IsAuthenticated && e.Account.Properties.ContainsKey("token"))
                    {
#if __IOS__
                        controller.DismissViewController(true, null);
#endif
                        var easyAuthResponseJson = JsonConvert.DeserializeObject <JObject> (e.Account.Properties ["token"]);
                        var easyAuthToken        = easyAuthResponseJson.GetValue("authenticationToken").ToString();

                        //call ResourceBroker to get the DocumentDB resource token
                        var http = new HttpClient();
                        http.DefaultRequestHeaders.Add("x-zumo-auth", easyAuthToken);
                        var resourceTokenResponse = await http.GetAsync(resourceTokenBrokerURL + "/api/resourcetoken/");

                        var resourceTokenJson = JsonConvert.DeserializeObject <JObject> (await resourceTokenResponse.Content.ReadAsStringAsync());
                        resourceToken = resourceTokenJson.GetValue("token").ToString();
                        UserId        = resourceTokenJson.GetValue("userid").ToString();

                        if (resourceToken == null)
                        {
                            return;                                                // failed to login
                        }
                        Client = new DocumentClient(new System.Uri(accountURL), resourceToken);

                        tcs.SetResult(true);
                    }
                };
#if __IOS__
                controller.PresentViewController(auth.GetUI(), true, null);
#else
                Xamarin.Forms.Forms.Context.StartActivity(auth.GetUI(Xamarin.Forms.Forms.Context));
#endif
            }
            catch (Exception ex) {
                Console.Error.WriteLine(@"ERROR Login {0}", ex.Message);
            }

            await tcs.Task;
        }
Ejemplo n.º 2
0
        public async Task <bool> LoginAsync(Xamarin.Forms.Page page)
        {
            string resourceToken = null;
            var    tcs           = new TaskCompletionSource <bool>();

#if __IOS__
            var controller = UIKit.UIApplication.SharedApplication.KeyWindow.RootViewController;
#endif
            try
            {
                var auth = new Xamarin.Auth.WebRedirectAuthenticator(
                    new Uri(Constants.ResourceTokenBrokerUrl + "/.auth/login/facebook"),
                    new Uri(Constants.ResourceTokenBrokerUrl + "/.auth/login/done"));

                auth.Completed += async(sender, e) =>
                {
                    if (e.IsAuthenticated && e.Account.Properties.ContainsKey("token"))
                    {
#if __IOS__
                        controller.DismissViewController(true, null);
#endif
                        var easyAuthResponseJson = JsonConvert.DeserializeObject <JObject>(e.Account.Properties["token"]);
                        var easyAuthToken        = easyAuthResponseJson.GetValue("authenticationToken").ToString();

                        // Call the ResourceBroker to get the DocumentDB resource token
                        using (var httpClient = new HttpClient())
                        {
                            httpClient.DefaultRequestHeaders.Add("x-zumo-auth", easyAuthToken);
                            var response = await httpClient.GetAsync(Constants.ResourceTokenBrokerUrl + "/api/resourcetoken/");

                            var jsonString = await response.Content.ReadAsStringAsync();

                            var tokenJson = JsonConvert.DeserializeObject <JObject>(jsonString);
                            resourceToken = tokenJson.GetValue("token").ToString();
                            UserId        = tokenJson.GetValue("userid").ToString();

                            if (!string.IsNullOrWhiteSpace(resourceToken))
                            {
                                client = new DocumentClient(new Uri(Constants.EndpointUri), resourceToken);
                                tcs.SetResult(true);
                            }
                            else
                            {
                                tcs.SetResult(false);
                            }
                        }
                    }
                };

#if __IOS__
                controller.PresentViewController(auth.GetUI(), true, null);
#elif __ANDROID__
                MainActivity.Instance.StartActivity(auth.GetUI(MainActivity.Instance));
#endif
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Error: ", ex.Message);
            }

            await tcs.Task;
            return(tcs.Task.Result);
        }