Example #1
0
        // This is called from m_clientSock_OnTaskCompleted and is therefore in the background thread.
        private void sendHttpGet()
        {
            string httpGetRequest = "GET / HTTP/1.1\r\nHost: www.bonairefishing.com\r\n\r\n";

            Chilkat.Task task = m_clientSock.SendStringAsync(httpGetRequest);
            task.UserData = "sendHttpGet";
            task.Run();
        }
Example #2
0
        private void do_oauth2(OAuth2Params p)
        {
            Chilkat.OAuth2 oauth2 = new Chilkat.OAuth2();

            oauth2.ListenPort = p.ListenPort;

            oauth2.AuthorizationEndpoint = p.AuthorizationEndpoint;
            oauth2.TokenEndpoint         = p.TokenEndpoint;
            oauth2.ClientId            = p.ClientId;
            oauth2.ClientSecret        = p.ClientSecret;
            oauth2.CodeChallenge       = true;
            oauth2.CodeChallengeMethod = "S256";
            if ((p.Scope != null) && (p.Scope.Length > 0))
            {
                oauth2.Scope = p.Scope;
            }

            // Begin the OAuth2 flow.  Returns a URL that should be loaded in a browser.
            string url = oauth2.StartAuth();

            if (url == null)
            {
                textBox1.Text = oauth2.LastErrorText;
                MessageBox.Show("StartAuth failed.");
                return;
            }

            m_oauth2 = oauth2;

            // Start a web browser and load the url.
            // This is where the end-user should accept or deny the authorization request.
            System.Diagnostics.Process.Start(url);

            // Monitor for the OAuth2 completion.
            m_oauth2.OnTaskCompleted += oauth2_OnTaskCompleted;
            Chilkat.Task task = m_oauth2.MonitorAsync();
            if (task == null)
            {
                MessageBox.Show("Failed to start monitoring.");
                return;
            }

            // Start the task.
            // oauth2_OnTaskCompleted will be called when the end-user responds from the browser.
            task.Run();

            // We're done with the .NET task object.
            // The underlying (internal) Chilkat task is running in a background thread.
            // Disposing of the .NET object does not affect the task that is running.
            // It is important to dispose of .NET's reference to the underlying object so that
            // when the task does complete, it is deallocated.  Otherwise, a reference
            // to the underlying task remains and would only get removed when .NET
            // garbage collects.
            task.Dispose();
            task = null;

            return;
        }
Example #3
0
        // Connect to an HTTP server asynchronously, send an HTTP GET request (async)
        // and receive the response (also async).
        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = "Starting Async Client Example...\r\n";

            // Connect to some website asynchronously.
            Chilkat.Task task = m_clientSock.ConnectAsync("www.bonairefishing.com", 80, false, 10000);
            task.UserData = "connect";
            task.Run();
        }
Example #4
0
        // URLs to help understand this topic:
        // https://developers.google.com/identity/protocols/OAuth2InstalledApp
        //
        private bool oauth2_google(string scope)
        {
            // Generates state and PKCE values.
            string state         = m_prng.GenRandom(32, base64Url);
            string code_verifier = m_prng.GenRandom(32, base64Url);

            Chilkat.Crypt2 crypt = new Chilkat.Crypt2();
            crypt.EncodingMode  = base64Url;
            crypt.HashAlgorithm = "SHA256";
            string       code_challenge        = crypt.HashStringENC(code_verifier);
            const string code_challenge_method = "S256";

            //Create a Chilkat socket for listening.  Begin listening asynchronously.
            Chilkat.Socket listenSocket = new Chilkat.Socket();
            int            backlog      = 5;
            int            listenPort   = 0;

            // Passing a listenPort = 0 causes BindAndListen to find a random unused port.
            // The chosen port will be available via the ListenPort property.
            if (!listenSocket.BindAndListen(listenPort, backlog))
            {
                fgAppendToErrorLog(listenSocket.LastErrorText);
                popupError("Failed to BindAndListen");
                return(false);
            }

            // Get the chosen listen port
            // This ListenPort property is available starting in Chilkat v9.5.0.59
            listenPort = listenSocket.ListenPort;

            // Creates a redirect URI using an available port on the loopback address.
            string redirect_uri = "http://127.0.0.1:" + listenPort.ToString() + "/";

            //Wait a max of 5 minutes.  The OnTaskCompleted event is called when an incoming connection
            //arrives, or when the listen failed.
            listenSocket.OnTaskCompleted += listenSocket_OnTaskCompleted;
            Chilkat.Task task = listenSocket.AcceptNextConnectionAsync(5 * 60000);
            if (task == null)
            {
                MessageBox.Show("Failed to start socket accept...");
                return(false);
            }

            // Add some information that will be needed by the TaskCompleted event..
            Chilkat.JsonObject taskData = new Chilkat.JsonObject();
            taskData.AppendString("code_verifier", code_verifier);
            taskData.AppendString("redirect_uri", redirect_uri);
            task.UserData = taskData.Emit();

            // Start the task.
            task.Run();

            // Creates the OAuth 2.0 authorization request.
            Chilkat.StringBuilder sbAuthRequest = new Chilkat.StringBuilder();
            sbAuthRequest.Append(authorizationEndpoint);
            sbAuthRequest.Append("?response_type=code&scope=");
            sbAuthRequest.Append(m_encoder.EncodeString(scope, "utf-8", "url"));
            sbAuthRequest.Append("&redirect_uri=");
            sbAuthRequest.Append(m_encoder.EncodeString(redirect_uri, "utf-8", "url"));
            sbAuthRequest.Append("&client_id=");
            sbAuthRequest.Append(googleAppClientId);
            sbAuthRequest.Append("&state=");
            sbAuthRequest.Append(state);
            sbAuthRequest.Append("&code_challenge=");
            sbAuthRequest.Append(code_challenge);
            sbAuthRequest.Append("&code_challenge_method=");
            sbAuthRequest.Append(code_challenge_method);

            // Here is a shorter way of building the URL in C#
            //string authorizationRequest = string.Format("{0}?response_type=code&scope={6}&redirect_uri={1}&client_id={2}&state={3}&code_challenge={4}&code_challenge_method={5}",
            //    authorizationEndpoint,  // 0
            //    System.Uri.EscapeDataString(redirect_uri), // 1
            //    googleAppClientId, // 2
            //    state, // 3
            //    code_challenge, // 4
            //    code_challenge_method, // 5
            //    System.Uri.EscapeDataString(scope)); // 6

            // Get authorization from Google account owner...
            webBrowser1.Navigate(sbAuthRequest.GetAsString());

            return(true);
        }
Example #5
0
 // This is called from m_clientSock_OnTaskCompleted and is therefore in the background thread.
 private void readHttpResponseBody(int contentLength)
 {
     Chilkat.Task task = m_clientSock.ReceiveBytesNAsync((uint)contentLength);
     task.UserData = "readResponseBody";
     task.Run();
 }
Example #6
0
 // This is called from m_clientSock_OnTaskCompleted and is therefore in the background thread.
 private void readHttpResponseHeader()
 {
     Chilkat.Task task = m_clientSock.ReceiveUntilMatchAsync("\r\n\r\n");
     task.UserData = "readResponseHeader";
     task.Run();
 }