Пример #1
0
        private void oauth2BeginButton_Click(object sender, RoutedEventArgs e)
        {
            var authServer = new DotNetOpenAuth.OAuth2.AuthorizationServerDescription {
                AuthorizationEndpoint = new Uri(this.oauth2AuthorizationUrlBox.Text),
            };

            if (this.oauth2TokenEndpointBox.Text.Length > 0)
            {
                authServer.TokenEndpoint = new Uri(this.oauth2TokenEndpointBox.Text);
            }

            try {
                var client = new OAuth2.UserAgentClient(authServer, this.oauth2ClientIdentifierBox.Text, this.oauth2ClientSecretBox.Text);

                var authorizePopup = new Authorize2(client);
                authorizePopup.Authorization.Scope.AddRange(OAuthUtilities.SplitScopes(this.oauth2ScopeBox.Text));
                authorizePopup.Owner = this;
                bool?result = authorizePopup.ShowDialog();
                if (result.HasValue && result.Value)
                {
                    var requestUri = new UriBuilder(this.oauth2ResourceUrlBox.Text);
                    if (this.oauth2ResourceHttpMethodList.SelectedIndex > 0)
                    {
                        requestUri.AppendQueryArgument("access_token", authorizePopup.Authorization.AccessToken);
                    }

                    var request = (HttpWebRequest)WebRequest.Create(requestUri.Uri);
                    request.Method = this.oauth2ResourceHttpMethodList.SelectedIndex < 2 ? "GET" : "POST";
                    if (this.oauth2ResourceHttpMethodList.SelectedIndex == 0)
                    {
                        client.AuthorizeRequest(request, authorizePopup.Authorization);
                    }

                    using (var resourceResponse = request.GetResponse()) {
                        using (var responseStream = new StreamReader(resourceResponse.GetResponseStream())) {
                            this.oauth2ResultsBox.Text = responseStream.ReadToEnd();
                        }
                    }
                }
                else
                {
                    return;
                }
            } catch (Messaging.ProtocolException ex) {
                MessageBox.Show(this, ex.Message);
            } catch (WebException ex) {
                string responseText = string.Empty;
                if (ex.Response != null)
                {
                    using (var responseReader = new StreamReader(ex.Response.GetResponseStream())) {
                        responseText = responseReader.ReadToEnd();
                    }
                }
                MessageBox.Show(this, ex.Message + "  " + responseText);
            }
        }
Пример #2
0
        private void beginWcfAuthorizationButton_Click(object sender, RoutedEventArgs e)
        {
            var auth = new Authorize2(this.wcf);

            auth.Authorization.Scope.AddRange(OAuthUtilities.SplitScopes("http://tempuri.org/IDataApi/GetName http://tempuri.org/IDataApi/GetAge http://tempuri.org/IDataApi/GetFavoriteSites"));
            auth.Authorization.Callback = new Uri("http://localhost:59721/");
            auth.Owner = this;
            bool?result = auth.ShowDialog();

            if (result.HasValue && result.Value)
            {
                this.wcfAccessToken           = auth.Authorization;
                this.wcfName.Content          = this.CallService(client => client.GetName());
                this.wcfAge.Content           = this.CallService(client => client.GetAge());
                this.wcfFavoriteSites.Content = this.CallService(client => string.Join(", ", client.GetFavoriteSites()));
            }
        }
Пример #3
0
        private async void oauth2BeginButton_Click(object sender, RoutedEventArgs e)
        {
            var authServer = new DotNetOpenAuth.OAuth2.AuthorizationServerDescription {
                AuthorizationEndpoint = new Uri(this.oauth2AuthorizationUrlBox.Text),
            };

            if (this.oauth2TokenEndpointBox.Text.Length > 0)
            {
                authServer.TokenEndpoint = new Uri(this.oauth2TokenEndpointBox.Text);
            }

            try {
                var client = new OAuth2.UserAgentClient(authServer, this.oauth2ClientIdentifierBox.Text, this.oauth2ClientSecretBox.Text);

                var authorizePopup = new Authorize2(client);
                authorizePopup.Authorization.Scope.AddRange(OAuthUtilities.SplitScopes(this.oauth2ScopeBox.Text));
                authorizePopup.Authorization.Callback = new Uri("http://www.microsoft.com/en-us/default.aspx");
                authorizePopup.Owner = this;
                authorizePopup.ClientAuthorizationView.RequestImplicitGrant = this.flowBox.SelectedIndex == 1;
                bool?result = authorizePopup.ShowDialog();
                if (result.HasValue && result.Value)
                {
                    var request = new HttpRequestMessage(
                        new HttpMethod(((ComboBoxItem)this.oauth2ResourceHttpMethodList.SelectedValue).Content.ToString()),
                        this.oauth2ResourceUrlBox.Text);
                    using (var httpClient = new HttpClient(client.CreateAuthorizingHandler(authorizePopup.Authorization))) {
                        using (var resourceResponse = await httpClient.SendAsync(request)) {
                            this.oauth2ResultsBox.Text = await resourceResponse.Content.ReadAsStringAsync();
                        }
                    }
                }
            } catch (Messaging.ProtocolException ex) {
                MessageBox.Show(this, ex.Message);
            } catch (WebException ex) {
                string responseText = string.Empty;
                if (ex.Response != null)
                {
                    using (var responseReader = new StreamReader(ex.Response.GetResponseStream())) {
                        responseText = responseReader.ReadToEnd();
                    }
                }
                MessageBox.Show(this, ex.Message + "  " + responseText);
            }
        }