Beispiel #1
0
        private void newAccessTokenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ClientInformation newClientInfo;

            AccessTokenWizard accessTokenWizard = new AccessTokenWizard();

            if (accessTokenWizard.ShowDialog(out newClientInfo) == DialogResult.OK)
            {
                // Override current info.
                clientInfo = newClientInfo;

                if (clientInfo.AuthType == AuthEndpoints.Basic)
                {
                    // Basic auth

                    textBox_BasicAuthSMTPAddress.Enabled            = true;
                    textBox_BasicAuthSMTPAddress.Text               = "";
                    textBox_BasicAuthPassword.Enabled               = true;
                    textBox_BasicAuthPassword.Text                  = "";
                    textBox_BasicAuthPassword.UseSystemPasswordChar = true;
                }
                else if (clientInfo.AuthType == AuthEndpoints.OAuthV1)
                {
                    // OAuth V1

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

                    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 = !string.IsNullOrEmpty(clientInfo.Token.access_token);
                button_RefreshToken.Enabled  = !string.IsNullOrEmpty(clientInfo.Token.refresh_token);
                button_Run.Enabled           = true;

                // Select the Body page.
                tabControl_Request.SelectTab(1);
            }
        }
        private void newAccessTokenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ClientInformation newClientInfo;

            AccessTokenWizard accessTokenWizard = new AccessTokenWizard();

            if (accessTokenWizard.ShowDialog(out newClientInfo) == DialogResult.OK)
            {
                // Override current info.
                clientInfo = newClientInfo;

                if (clientInfo.AuthType == AuthEndpoints.Basic)
                {
                    // Basic auth

                    useBasicAuth = true;
                    textBox_BasicAuthSMTPAddress.Enabled            = true;
                    textBox_BasicAuthSMTPAddress.Text               = "";
                    textBox_BasicAuthPassword.Enabled               = true;
                    textBox_BasicAuthPassword.Text                  = "";
                    textBox_BasicAuthPassword.UseSystemPasswordChar = true;
                }
                else if (clientInfo.AuthType == AuthEndpoints.OAuthV1)
                {
                    // OAuth V1

                    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;
                }
                else
                {
                    // OAuth V2

                    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;
                }



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

                button_ViewTokenInfo.Enabled = !string.IsNullOrEmpty(clientInfo.Token.access_token);
                button_RefreshToken.Enabled  = !string.IsNullOrEmpty(clientInfo.Token.refresh_token);
                button_Run.Enabled           = true;

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

            //AcquireAccessToken();
        }
Beispiel #3
0
        private async void button_RefreshToken_Click(object sender, EventArgs e)
        {
            // Request another access token with refresh token.

            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(clientInfo.ResourceUri);

                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"] = System.Web.HttpUtility.UrlEncode(clientInfo.ClientSecret);
                }
            }
            else
            {
                endPoint                 += "v2.0/";
                tempTable["scope"]        = clientInfo.Scopes;
                tempTable["client_id"]    = clientInfo.ClientID;
                tempTable["redirect_uri"] = System.Web.HttpUtility.UrlEncode(clientInfo.RedirectUri);

                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 = "";

                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(CreateStatusCodeString(response), response.Headers, jsonResponse);

                // Deserialize and get Access Token.
                clientInfo.ReplaceToken(AccessTokenWizard.Deserialize <TokenResponse>(jsonResponse));
            }
            catch (WebException ex)
            {
                if (ex.Response == null)
                {
                    // Logging
                    if (checkBox_Logging.Checked)
                    {
                        Util.WriteCustomLog("Response", ex.Message);
                    }

                    DisplayResponse("Error", null, ex.Message);
                }
                else
                {
                    string jsonResponse = "";
                    using (Stream responseStream = ex.Response.GetResponseStream())
                    {
                        StreamReader reader = new StreamReader(responseStream, Encoding.Default);
                        jsonResponse = reader.ReadToEnd();
                    }

                    HttpWebResponse response = (HttpWebResponse)ex.Response;

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

                    DisplayResponse(CreateStatusCodeString(response), ex.Response.Headers, jsonResponse);
                }
            }
            catch (Exception ex)
            {
                // Logging
                if (checkBox_Logging.Checked)
                {
                    Util.WriteCustomLog("Response", ex.Message);
                }

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