public async Task<string> ExecuteAsync(WebView webView, Grid owner)
        {
            var url = string.Format("{0}?response_type=code&client_id={1}&redirect_uri={2}", OAuthSettings.AuthorizeEndpoint,
               Uri.EscapeDataString(OAuthSettings.ClientId),
               Uri.EscapeDataString("https://returnurl"));


            TaskCompletionSource<string> tcs = new TaskCompletionSource<string>();


            webView.NavigationStarting += async (s, a) =>
            {
                if (a.Uri.ToString().StartsWith("https://returnurl"))
                {
                    // detect return url
                    owner.Children.Remove(webView);
                    if (a.Uri.Query.StartsWith("?code="))
                    {
                        var code = Uri.UnescapeDataString(a.Uri.Query.Substring(6));
                        using (var client = new HttpClient())
                        {
                            var content = new HttpFormUrlEncodedContent(new Dictionary<string, string>{
                                {"grant_type","authorization_code"},
                                {"code", code},
                                {"redirect_uri", "https://returnurl"},
                                {"client_id", OAuthSettings.ClientId},
                                {"client_secret", OAuthSettings.ClientSecret}
                            });
                            // exchange authorize code for an access token
                            var response = await client.PostAsync(new Uri(OAuthSettings.TokenEndpoint), content);
                            response.EnsureSuccessStatusCode();
                            var contentString = await response.Content.ReadAsStringAsync();
                            var accessTokenInfo = await JsonConvert.DeserializeObjectAsync<OAuthTokenInfo>(contentString);
                            OAuthSettings.AccessToken = accessTokenInfo.AccessToken;
                            OAuthSettings.RefreshToken = accessTokenInfo.RefreshToken;

                            tcs.SetResult(accessTokenInfo.AccessToken);
                        }
                    }
                }
            };
            webView.NavigateWithHttpRequestMessage(new HttpRequestMessage(HttpMethod.Get, new Uri(url)));


            return await tcs.Task;
        }
        private async void OnWebviewClick(object sender, RoutedEventArgs e)
        {
            await EnsureLoggedInAsync();

            var webView = new WebView() { Margin = new Thickness(50) };

            webView.Loaded += (s, a) =>
            {
                var message = new HttpRequestMessage(HttpMethod.Get, new Uri("http://localhost:20394/demowebviewauth"));
                //message.Headers.Authorization = new HttpCredentialsHeaderValue("Bearer", OAuthSettings.AccessToken);
                webView.NavigateWithHttpRequestMessage(message);
            };

            Grid.SetRow(webView, 1);

            layoutRoot.Children.Add(webView);
            await Task.Delay(15000);
            layoutRoot.Children.Remove(webView);
        }
 public void RefreshCookies()
 {
     LoggingService.Log("Attempting at refreshing cookies", LoggingLevel.Verbose);
     if (Window.Current == null)
         return;
     Account account = AccountManager.GetAccount();
     if (account != null)
     {
         var loginUri = new Uri(account.LoginUrl);
         var instanceUri = new Uri(account.InstanceUrl);
         var filter = new HttpBaseProtocolFilter();
         var cookie = new HttpCookie("salesforce", loginUri.Host, "/");
         var instance = new HttpCookie("salesforceInstance", instanceUri.Host, "/");
         cookie.Value = account.AccessToken;
         instance.Value = account.AccessToken;
         filter.CookieManager.SetCookie(cookie, false);
         filter.CookieManager.SetCookie(instance, false);
         var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, instanceUri);
         var web = new WebView();
         web.NavigateWithHttpRequestMessage(httpRequestMessage);
     }
     LoggingService.Log("finished refreshing cookies", LoggingLevel.Verbose);
 }
 public static void RefreshCookies()
 {
     PlatformAdapter.SendToCustomLogger("OAuth.RefreshCookies - attempting at refreshing cookies", LoggingLevel.Verbose);
     Account account = AccountManager.GetAccount();
     if (account != null)
     {
         var loginUri = new Uri(account.LoginUrl);
         var instanceUri = new Uri(account.InstanceUrl);
         var filter = new HttpBaseProtocolFilter();
         var cookie = new HttpCookie("salesforce", loginUri.Host, "/");
         var instance = new HttpCookie("salesforceInstance", instanceUri.Host, "/");
         cookie.Value = account.AccessToken;
         instance.Value = account.AccessToken;
         filter.CookieManager.SetCookie(cookie, false);
         filter.CookieManager.SetCookie(instance, false);
         var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, instanceUri);
         var web = new WebView();
         web.NavigateWithHttpRequestMessage(httpRequestMessage);
     }
     PlatformAdapter.SendToCustomLogger("OAuth.RefreshCookies - done", LoggingLevel.Verbose);
 }