box / box-windows-sdk-v2

Windows SDK for v2 of the Box API. The SDK is built upon .NET Framework 4.5
https://developer.box.com
Apache License 2.0
187 stars 163 forks source link

Setting a new WebProxy still uses the old proxy #583

Open rtrugman opened 4 years ago

rtrugman commented 4 years ago

Description of the Issue

Once you set a WebProxy and attempt authentication, setting a new proxy does not take effect. The code seems to still be using the old proxy.

Setting Proxy Address: proxyabc Error: One or more errors occurred. An error occurred while sending the request. The remote name could not be resolved: 'proxyabc'. Setting Proxy Address: proxyxyz Error: One or more errors occurred. An error occurred while sending the request. The remote name could not be resolved: 'proxyabc'.

Versions Used

.NET 4.51:

Steps to Reproduce

Error Message, Including Stack Trace

mattwiller commented 4 years ago

@rtrugman Could you provide a bit more context to help us understand the issue you're experiencing? It would be really helpful to see how you're trying to set the proxy for the SDK to use in your application, so we can start troubleshooting what might be going on.

rtrugman commented 4 years ago

I have a .NET Forms UI program where the user can configure the Box settings including the proxy. If the user attempts authentication and then goes and changes the proxy, the next authentication attempt uses the first proxy setting. Does not matter is the first authentication succeeded or failed.

                //Set Proxy
                if ( theSettings.ProxyServer != String.Empty )
                {
                    Output( "Setting Proxy Address: {0}", theSettings.ProxyServer );

                    WebProxy theProxy = new WebProxy( theSettings.ProxyServer );

                    // Create a NetworkCredential object and associate it with the Proxy property of request object.
                    if ( theSettings.ProxyUserName != String.Empty )
                    {
                        Output( "Setting Proxy Credentials User: {0} Domain: {1}", theSettings.ProxyUserName, theSettings.ProxyDomain );

                        theProxy.Credentials = new NetworkCredential( theSettings.ProxyUserName, theSettings.ProxyPassword, theSettings.ProxyDomain );
                    }

                    config.WebProxy = theProxy;
                }
                else
                {
                    config.WebProxy = new WebProxy();
                }
            }

            // Create JWT auth using config file
            var boxJWT = new BoxJWTAuth( config );

            // Create admin client
            var adminToken = boxJWT.AdminToken();
            _boxclient = boxJWT.AdminClient( adminToken );
mattwiller commented 4 years ago

@rtrugman To clarify, what do you mean when you say the application "changes the proxy"? In your code sample, it's hard to tell what the overall flow is; the code appears to just set the proxy once. Are you re-creating a new client with a new config each time?

rtrugman commented 4 years ago

That's the code that gets executed each time the user authenticates with new settings. The user updates the settings and then the code in this method is called to authenticate. Here is the full method that's a WInForms link event.

private void linkLogOn_LinkClicked( object sender, LinkLabelLinkClickedEventArgs e )
    {
        this.listBox1.Items.Clear();
        this.Cursor = Cursors.WaitCursor;

        Output( "Connecting using config file : {0} ", this.txtConfigFile.Text );

        try
        {
            if (null != _boxclient)
            {
                _boxclient.Auth.LogoutAsync();
            }
            // Read in config file
            IBoxConfig config = null;
            using ( FileStream fs = new FileStream( this.txtConfigFile.Text, FileMode.Open ) )
            {
                config = BoxConfig.CreateFromJsonFile( fs );

                //Set Proxy
                if ( theSettings.ProxyServer != String.Empty )
                {
                    Output( "Setting Proxy Address: {0}", theSettings.ProxyServer );

                    WebProxy theProxy = new WebProxy( theSettings.ProxyServer );

                    // Create a NetworkCredential object and associate it with the Proxy property of request object.
                    if ( theSettings.ProxyUserName != String.Empty )
                    {
                        Output( "Setting Proxy Credentials User: {0} Domain: {1}", theSettings.ProxyUserName, theSettings.ProxyDomain );

                        theProxy.Credentials = new NetworkCredential( theSettings.ProxyUserName, theSettings.ProxyPassword, theSettings.ProxyDomain );
                    }

                    config.WebProxy = theProxy;
                }
                else
                {
                    config.WebProxy = new WebProxy();
                }
            }

            // Create JWT auth using config file
            var boxJWT = new BoxJWTAuth( config );

            // Create admin client
            var adminToken = boxJWT.AdminToken();
            _boxclient = boxJWT.AdminClient( adminToken );

            // Create Managed User client
            var userId = this.txtUserId.Text;
            if ( !String.IsNullOrEmpty( userId ) )
            {
                var auth = new Box.V2.Auth.OAuthSession( adminToken, "NOT NEEDED", 10, "bearer" );

                var userClient = new BoxClient( config, auth, asUser: userId );

                _boxclient = userClient;

            }
            if ( chkJSON.Checked )
            {
                string jsonData = Newtonsoft.Json.JsonConvert.SerializeObject( _boxclient );
                Output( "Json: {0}", jsonData );
            }
            Output( "Authentication Successful." );

            this.linkLogout.Enabled = true;

        }
        catch ( Exception ex )
        {
            Output( "Error: " + ex.MessageWithInner() );
            Output( ex.StackTrace );
        }
        Output( String.Empty );
        this.Cursor = Cursors.Default;
    }
mattwiller commented 4 years ago

@rtrugman Ah, I think I see what the issue is — the SDK uses a shared, static pool of HttpClient instances internally across all SDK clients; these are not re-initialized when the proxy is updated. I'll add this to our backlog to fix (SDK-1019), but you're of course welcome to make a PR of your own if you need the fix sooner than we can get to it. Either way, I'll keep this issue open to track progress, and we'll update here when we have a fix ready.

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not been updated in the last 30 days. It will be closed if no further activity occurs within the next 7 days. Feel free to reach out or mention Box SDK team member for further help and resources if they are needed.