コード例 #1
0
        private void AcquireAccessToken()
        {
            // get an access token.

            StartForm startForm = new StartForm();

            if (startForm.ShowDialog(out _tokenResponse, out _resource, out _clientID, out _clientSecret, out _scopes, out _redirectUri, out _useV2Endpoint) == DialogResult.OK)
            {
                if (_tokenResponse.access_token == null && _tokenResponse.id_token != null)
                {
                    // Using OpenID Connect
                    _tokenResponse.access_token = _tokenResponse.id_token;
                }

                if (_tokenResponse.access_token.StartsWith("USEBASICBASIC"))
                {
                    // Basic auth

                    useBasicAuth = true;
                    textBox_BasicAuthSMTPAddress.Enabled = true;
                    textBox_BasicAuthPassword.Enabled    = true;
                    button_ViewTokenInfo.Enabled         = false;
                    button_RefreshToken.Enabled          = false;
                }
                else if (_useV2Endpoint == false)
                {
                    // OAuth

                    useBasicAuth = false;
                    textBox_BasicAuthSMTPAddress.Enabled            = false;
                    textBox_BasicAuthSMTPAddress.Text               = "OAuth (V1 Endpoint)";
                    textBox_BasicAuthPassword.Enabled               = false;
                    textBox_BasicAuthPassword.Text                  = "OAuth (V1 Endpoint)";
                    textBox_BasicAuthPassword.UseSystemPasswordChar = false;
                    button_ViewTokenInfo.Enabled = true;
                }
                else
                {
                    // OAuth (V2 Endpoint)

                    useBasicAuth = false;
                    textBox_BasicAuthSMTPAddress.Enabled            = false;
                    textBox_BasicAuthSMTPAddress.Text               = "OAuth (V2 Endpoint)";
                    textBox_BasicAuthPassword.Enabled               = false;
                    textBox_BasicAuthPassword.Text                  = "OAuth (V2 Endpoint)";
                    textBox_BasicAuthPassword.UseSystemPasswordChar = false;
                    button_ViewTokenInfo.Enabled = true;
                }

                if (string.IsNullOrEmpty(_tokenResponse.refresh_token))
                {
                    button_RefreshToken.Enabled = false;
                }

                // Select the Body page.
                tabControl_HeadersAndBody.SelectTab(1);
            }
        }
コード例 #2
0
        private void RequestForm_Load(object sender, EventArgs e)
        {
            // First of all, we have to get an access token.

            StartForm startForm = new StartForm();

            if (startForm.ShowDialog(out _tokenResponse, out _resource, out _clientID, out _clientSecret, out _scopes, out _redirectUri, out _useV2Endpoint) == DialogResult.OK)
            {
                if (_tokenResponse.access_token.StartsWith("USEBASICBASIC"))
                {
                    // Basic auth

                    useBasicAuth = true;
                    textBox_BasicAuthSMTPAddress.Enabled = true;
                    textBox_BasicAuthPassword.Enabled    = true;
                    button_ViewTokenInfo.Enabled         = false;
                    button_RefreshToken.Enabled          = false;
                }
                else if (_useV2Endpoint == false)
                {
                    // OAuth

                    useBasicAuth = false;
                    textBox_BasicAuthSMTPAddress.Enabled            = false;
                    textBox_BasicAuthSMTPAddress.Text               = "OAuth (V1 Endpoint)";
                    textBox_BasicAuthPassword.Enabled               = false;
                    textBox_BasicAuthPassword.Text                  = "OAuth (V1 Endpoint)";
                    textBox_BasicAuthPassword.UseSystemPasswordChar = false;
                    button_ViewTokenInfo.Enabled = true;
                }
                else
                {
                    // OAuth (V2 Endpoint)

                    useBasicAuth = false;
                    textBox_BasicAuthSMTPAddress.Enabled            = false;
                    textBox_BasicAuthSMTPAddress.Text               = "OAuth (V2 Endpoint)";
                    textBox_BasicAuthPassword.Enabled               = false;
                    textBox_BasicAuthPassword.Text                  = "OAuth (V2 Endpoint)";
                    textBox_BasicAuthPassword.UseSystemPasswordChar = false;
                    button_ViewTokenInfo.Enabled = true;
                }

                if (string.IsNullOrEmpty(_tokenResponse.refresh_token))
                {
                    button_RefreshToken.Enabled = false;
                }

                // Select the Body page.
                tabControl_HeadersAndBody.SelectTab(1);
            }
            else
            {
                this.Close();
            }
        }
コード例 #3
0
        private void button_RefreshToken_Click(object sender, EventArgs e)
        {
            // Request another access token with refresh token.

            originalResponseHeaders        = "";
            originalJsonResponse           = "";
            indentedJsonResponse           = "";
            decodedJsonResponse            = "";
            indentedAndDecodedJsonResponse = "";

            string endPoint = "https://login.microsoftonline.com/common/oauth2/";

            // Build a POST body.
            string    postBody  = "";
            Hashtable tempTable = new Hashtable();

            tempTable["grant_type"]    = "refresh_token";
            tempTable["refresh_token"] = _tokenResponse.refresh_token;

            if (_useV2Endpoint == false)
            {
                string resourceURL = StartForm.GetResourceURL(_resource);
                tempTable["resource"] = System.Web.HttpUtility.UrlEncode(resourceURL);

                if (_clientID != "")
                {
                    // If _clientID has value, we're working with web app.
                    // So we have to add Client ID and Client Secret.
                    tempTable["client_id"]     = _clientID;
                    tempTable["client_secret"] = _clientSecret;
                }
            }
            else
            {
                endPoint                 += "v2.0/";
                tempTable["scope"]        = _scopes;
                tempTable["client_id"]    = _clientID;
                tempTable["redirect_uri"] = _redirectUri;

                if (_clientID != "")
                {
                    // If _clientID has value, we're working with web app.
                    // So we have to add Client Secret.
                    tempTable["client_secret"] = _clientSecret;
                }
            }

            foreach (string key in tempTable.Keys)
            {
                postBody += String.Format("{0}={1}&", key, tempTable[key]);
            }
            byte[] postDataBytes = Encoding.ASCII.GetBytes(postBody);

            System.Net.WebRequest request = System.Net.WebRequest.Create(endPoint + "token/");
            request.Method        = "POST";
            request.ContentType   = "application/x-www-form-urlencoded";
            request.ContentLength = postDataBytes.Length;

            try
            {
                // Change a cursor.
                this.Cursor = Cursors.WaitCursor;

                // Get a RequestStream to POST a data.
                using (Stream reqStream = request.GetRequestStream())
                {
                    reqStream.Write(postDataBytes, 0, postDataBytes.Length);
                }

                string jsonResponse = "";

                System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();

                using (Stream responseStream = response.GetResponseStream())
                {
                    StreamReader reader = new StreamReader(responseStream, Encoding.Default);
                    jsonResponse = reader.ReadToEnd();
                }

                // Display the results.
                originalResponseHeaders  = "StatusCode : " + response.StatusCode.ToString() + "\r\n\r\n";
                originalResponseHeaders += "Response Header : \r\n" + response.Headers.ToString() + "\r\n\r\n";

                originalJsonResponse = jsonResponse;

                // Parse the JSON data.
                textBox_Result.Text = originalResponseHeaders + shapeJsonResponseIfNeeded(originalJsonResponse);

                // Deserialize and get Access Token.
                _tokenResponse = StartForm.Deserialize <TokenResponse>(jsonResponse);
            }
            catch (System.Net.WebException ex)
            {
                textBox_Result.Text = ex.Message + "\r\n\r\nResponse Headers : \r\n" + ex.Response.Headers.ToString();
            }
            catch (Exception ex)
            {
                textBox_Result.Text = ex.Message;
            }
            finally
            {
                // Change cursor.
                this.Cursor = Cursors.Default;
            }
        }
コード例 #4
0
        private bool AcquireAccessToken2()
        {
            // get an access token.

            StartForm startForm = new StartForm();

            if (startForm.ShowDialog(out clientInfo) == DialogResult.OK)
            {
                if (clientInfo.Token.access_token.StartsWith("USEBASICBASIC"))
                {
                    // Basic auth

                    useBasicAuth = true;
                    textBox_BasicAuthSMTPAddress.Enabled = true;
                    textBox_BasicAuthPassword.Enabled    = true;
                    button_ViewTokenInfo.Enabled         = false;
                    button_RefreshToken.Enabled          = false;
                }
                else if (clientInfo.AuthType == AuthEndpoints.OAuthV1)
                {
                    // OAuth

                    useBasicAuth = false;
                    textBox_BasicAuthSMTPAddress.Enabled            = false;
                    textBox_BasicAuthSMTPAddress.Text               = "OAuth (V1 Endpoint)";
                    textBox_BasicAuthPassword.Enabled               = false;
                    textBox_BasicAuthPassword.Text                  = "OAuth (V1 Endpoint)";
                    textBox_BasicAuthPassword.UseSystemPasswordChar = false;
                    button_ViewTokenInfo.Enabled = true;
                }
                else
                {
                    // OAuth (V2 Endpoint)

                    useBasicAuth = false;
                    textBox_BasicAuthSMTPAddress.Enabled            = false;
                    textBox_BasicAuthSMTPAddress.Text               = "OAuth (V2 Endpoint)";
                    textBox_BasicAuthPassword.Enabled               = false;
                    textBox_BasicAuthPassword.Text                  = "OAuth (V2 Endpoint)";
                    textBox_BasicAuthPassword.UseSystemPasswordChar = false;
                    button_ViewTokenInfo.Enabled = true;
                }

                if (string.IsNullOrEmpty(clientInfo.Token.refresh_token))
                {
                    button_RefreshToken.Enabled = false;
                }

                _tokenResponse = clientInfo.Token;
                _resource      = clientInfo.ResourceUri;
                _clientID      = clientInfo.ClientID;
                _clientSecret  = clientInfo.ClientSecret;
                _scopes        = clientInfo.Scopes;
                _redirectUri   = clientInfo.RedirectUri;
                _useV2Endpoint = (clientInfo.AuthType == AuthEndpoints.OAuthV2);

                // Select the Body page.
                tabControl_HeadersAndBody.SelectTab(1);

                return(true);
            }
            else
            {
                return(false);
            }
        }
コード例 #5
0
        private async void button_RefreshToken_Click(object sender, EventArgs e)
        {
            // Request another access token with refresh token.

            originalResponseHeaders        = "";
            originalJsonResponse           = "";
            indentedJsonResponse           = "";
            decodedJsonResponse            = "";
            indentedAndDecodedJsonResponse = "";

            string endPoint = "https://login.microsoftonline.com/common/oauth2/";

            // Build a POST body.
            string    postBody  = "";
            Hashtable tempTable = new Hashtable();

            tempTable["grant_type"]    = "refresh_token";
            tempTable["refresh_token"] = clientInfo.Token.refresh_token;

            if (clientInfo.AuthType == AuthEndpoints.OAuthV1)
            {
                string resourceURL = StartForm.GetResourceURL(clientInfo.ResourceUri);
                tempTable["resource"] = System.Web.HttpUtility.UrlEncode(resourceURL);

                if (clientInfo.ClientID != "")
                {
                    // If _clientID has value, we're working with web app.
                    // So we have to add Client ID and Client Secret.
                    tempTable["client_id"]     = clientInfo.ClientID;
                    tempTable["client_secret"] = clientInfo.ClientSecret;
                }
            }
            else
            {
                endPoint                 += "v2.0/";
                tempTable["scope"]        = clientInfo.Scopes;
                tempTable["client_id"]    = clientInfo.ClientID;
                tempTable["redirect_uri"] = clientInfo.ResourceUri;

                if (clientInfo.ClientID != "")
                {
                    // If _clientID has value, we're working with web app.
                    // So we have to add Client Secret.
                    tempTable["client_secret"] = clientInfo.ClientSecret;
                }
            }

            foreach (string key in tempTable.Keys)
            {
                postBody += String.Format("{0}={1}&", key, tempTable[key]);
            }
            byte[] postDataBytes = Encoding.ASCII.GetBytes(postBody);

            WebRequest request = WebRequest.Create(endPoint + "token/");

            request.Method        = "POST";
            request.ContentType   = "application/x-www-form-urlencoded";
            request.ContentLength = postDataBytes.Length;

            try
            {
                // Change a cursor.
                Application.UseWaitCursor = true;

                // Get a RequestStream to POST a data.
                using (Stream reqStream = request.GetRequestStream())
                {
                    reqStream.Write(postDataBytes, 0, postDataBytes.Length);
                }

                // Logging
                if (checkBox_Logging.Checked)
                {
                    WriteRequestLog(request, postBody);
                }

                string jsonResponse = "";

                // System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
                var response = (HttpWebResponse)await request.GetResponseAsync();

                using (Stream responseStream = response.GetResponseStream())
                {
                    StreamReader reader = new StreamReader(responseStream, Encoding.Default);
                    jsonResponse = reader.ReadToEnd();
                }

                // Logging
                if (checkBox_Logging.Checked)
                {
                    WriteResponseLog(response, jsonResponse);
                }

                // Display the results.
                DisplayResponse(response.StatusCode.ToString(), response.Headers, jsonResponse);

                // Deserialize and get Access Token.
                clientInfo.ReplaceToken(StartForm.Deserialize <TokenResponse>(jsonResponse));
            }
            catch (WebException ex)
            {
                string jsonResponse = "";
                using (Stream responseStream = ex.Response.GetResponseStream())
                {
                    StreamReader reader = new StreamReader(responseStream, Encoding.Default);
                    jsonResponse = reader.ReadToEnd();
                }

                // Logging
                if (checkBox_Logging.Checked)
                {
                    WriteResponseLog((HttpWebResponse)ex.Response, jsonResponse);
                }

                DisplayResponse(((HttpWebResponse)ex.Response).StatusCode.ToString(), ex.Response.Headers, jsonResponse);
            }
            catch (Exception ex)
            {
                // Logging
                if (checkBox_Logging.Checked)
                {
                    WriteCustomLog("Response", ex.Message);
                }

                DisplayResponse("Error", null, ex.Message);
            }
            finally
            {
                // Change cursor.
                Application.UseWaitCursor = false;
            }
        }