Sharpbox throw WebException with Mono, works in .NET

Jan 11, 2011 at 8:01 PM
Edited Jan 11, 2011 at 8:03 PM

Hi, I'm using this library to build a CLI app in C# that uploads a file into my Public Dropbox folder.
I downloaded the binary files (version 1.0.3-261, for FX 3.5) and added a reference to AppLimit.CloudComputing.SharpBox.dll (which in turn adds AppLimit.CloudComputing.oAuth.dll and Newtonsoft.Json.Net20.dll references) into my project.

The program indeed is very simple, here is the code:

 

using System;
using AppLimit.CloudComputing.SharpBox;
using AppLimit.CloudComputing.SharpBox.DropBox;

namespace DropBoxUploadTest
{
    class Program
    {
        static void Main(string[] args)
        {
            CloudStorage x = new CloudStorage();
            DropBoxCredentials cred = new DropBoxCredentials();

            cred.ConsumerKey = "***************";
            cred.ConsumerSecret = "***************";
            cred.UserName = "xxxxxx@yyyy.com";
            cred.Password = "1234512345";

            x.Open(DropBoxConfiguration.GetStandardConfiguration(), cred);
            x.UploadFile(args[0], x.GetFolder("/Public"));
            x.Close();
        }
    }
}

 

After compiling with VS2008, it runs perfectly under Windows with .NET 3.5, and the filename given as argument is copied to the Public folder of my Dropbox account.
Then I copied the compiled files to my Linux PC and tried to run the program with Mono version 2.6 and it always throws an exception:


Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object
  at AppLimit.CloudComputing.OAuth.OAuthService.PerformWebRequest (System.String url, System.String method, System.Collections.Generic.Dictionary`2 multiFormDataStream) [0x00000] in <filename unknown>:0
  at AppLimit.CloudComputing.OAuth.OAuthService.PerformTokenRequest (System.String requestTokenUrl) [0x00000] in <filename unknown>:0
  at AppLimit.CloudComputing.OAuth.OAuthService.GetRequestToken (AppLimit.CloudComputing.OAuth.Context.OAuthServiceContext svcContext, AppLimit.CloudComputing.OAuth.Context.OAuthConsumerContext conContext) [0x00000] in <filename unknown>:0
  at AppLimit.CloudComputing.SharpBox.DropBox.Logic.DropBoxApplication.AuthorizeAndGetSession (System.String userName, System.String password) [0x00000] in <filename unknown>:0
  at AppLimit.CloudComputing.SharpBox.DropBox.Logic.DropBoxApplication.Authorize (System.String userName, System.String password) [0x00000] in <filename unknown>:0
  at AppLimit.CloudComputing.SharpBox.DropBox.DropBoxStorageProvider.Open (ICloudStorageConfiguration configuration, ICloudStorageCredentials credentials) [0x00000] in <filename unknown>:0
  at AppLimit.CloudComputing.SharpBox.CloudStorage.Open (ICloudStorageConfiguration configuration, ICloudStorageCredentials credentials) [0x00000] in <filename unknown>:0
  at DropBoxUploadTest.Program.Main(String[] args) [0x00000] in <filename unknown>:0

 

I tried installing Mono for Windows into a Virtual Machine, and still the program fails under Mono but works under Microsoft.NET.
What am I doing wrong? According to here, the library should be compatible with Mono. Any clues?

Thank you.

P.S.: Wonderful library, I just hope I can make it work under Linux!

Coordinator
Jan 11, 2011 at 8:08 PM
Hi,

please open an issue, I will figure out the issue in the next 2 days

Dirk

Sent from my iPhone

On 11.01.2011, at 21:02, virtualdj <notifications@codeplex.com> wrote:

From: virtualdj

Hi, I'm using this library to build a CLI app in C# that uploads a file into my Public Dropbox folder.
I downloaded the binary files (version 1.0.3-261, for FX 3.5) and added a reference to AppLimit.CloudComputing.SharpBox.dll (which in turn adds AppLimit.CloudComputing.oAuth.dll and Newtonsoft.Json.Net20.dll references) into my project.

The program indeed is very simple, here is the code:

using System;
using AppLimit.CloudComputing.SharpBox;
using AppLimit.CloudComputing.SharpBox.DropBox;

namespace DropBoxUploadTest
{
    class Program
    {
        static void Main(string[] args)
        {
            CloudStorage x = new CloudStorage();
            DropBoxCredentials cred = new DropBoxCredentials();

            cred.ConsumerKey = "***************";
            cred.ConsumerSecret = "***************";
            cred.UserName = "xxxxxx@yyyy.com";
            cred.Password = "1234512345";

            x.Open(DropBoxConfiguration.GetStandardConfiguration(), cred);
            x.UploadFile(args[0], x.GetFolder("/Public"));
            x.Close();
        }
    }
}

After compiling with VS2008, it runs perfectly under Windows with .NET 3.5, and the filename given as argument is copied to the Public folder of my Dropbox account.
Then I copied the compiled files to my Linux PC and tried to run the program with Mono version 2.6 and it always throws an exception:


Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object
at AppLimit.CloudComputing.OAuth.OAuthService.PerformWebRequest (System.String url, System.String method, System.Collections.Generic.Dictionary`2 multiFormDataStream) [0x00000] in <filename unknown>:0
at AppLimit.CloudComputing.OAuth.OAuthService.PerformTokenRequest (System.String requestTokenUrl) [0x00000] in <filename unknown>:0
at AppLimit.CloudComputing.OAuth.OAuthService.GetRequestToken (AppLimit.CloudComputing.OAuth.Context.OAuthServiceContext svcContext, AppLimit.CloudComputing.OAuth.Context.OAuthConsumerContext conContext) [0x00000] in <filename unknown>:0
at AppLimit.CloudComputing.SharpBox.DropBox.Logic.DropBoxApplication.AuthorizeAndGetSession (System.String userName, System.String password) [0x00000] in <filename unknown>:0
at AppLimit.CloudComputing.SharpBox.DropBox.Logic.DropBoxApplication.Authorize (System.String userName, System.String password) [0x00000] in <filename unknown>:0
at AppLimit.CloudComputing.SharpBox.DropBox.DropBoxStorageProvider.Open (ICloudStorageConfiguration configuration, ICloudStorageCredentials credentials) [0x00000] in <filename unknown>:0
at AppLimit.CloudComputing.SharpBox.CloudStorage.Open (ICloudStorageConfiguration configuration, ICloudStorageCredentials credentials) [0x00000] in <filename unknown>:0
at DropBoxUploadTest.Program.Main(String[] args) [0x00000] in <filename unknown>:0

I tried installing Mono for Windows into a Virtual Machine, and still the program fails under Mono but works under Microsoft.NET.
What am I doing wrong? According to here, the library should be compatible with Mono. Any clues?

Thank you.

Jan 12, 2011 at 7:09 PM

Hi, I was doing more testing on the app to publish the issue and unexpectly I have found the solution!
The exception thrown was:

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object
  at AppLimit.CloudComputing.OAuth.OAuthService.PerformWebRequest (System.String url, System.String method, System.Collections.Generic.Dictionary`2 multiFormDataStream) [0x00000] in <filename unknown>:0
  at AppLimit.CloudComputing.OAuth.OAuthService.PerformTokenRequest (System.String requestTokenUrl) [0x00000] in <filename unknown>:0
  at AppLimit.CloudComputing.OAuth.OAuthService.GetRequestToken (AppLimit.CloudComputing.OAuth.Context.OAuthServiceContext svcContext, AppLimit.CloudComputing.OAuth.Context.OAuthConsumerContext conContext) [0x00000] in <filename unknown>:0
  at AppLimit.CloudComputing.SharpBox.DropBox.Logic.DropBoxApplication.AuthorizeAndGetSession (System.String userName, System.String password) [0x00000] in <filename unknown>:0
  at AppLimit.CloudComputing.SharpBox.DropBox.Logic.DropBoxApplication.Authorize (System.String userName, System.String password) [0x00000] in <filename unknown>:0
  at AppLimit.CloudComputing.SharpBox.DropBox.DropBoxStorageProvider.Open (ICloudStorageConfiguration configuration, ICloudStorageCredentials credentials) [0x00000] in <filename unknown>:0
  at AppLimit.CloudComputing.SharpBox.CloudStorage.Open (ICloudStorageConfiguration configuration, ICloudStorageCredentials credentials) [0x00000] in <filename unknown>:0
  at DropBoxUploadTest.Program.Main (System.String[] args) [0x00000] in <filename unknown>:0

It wasn't so useful, so I downloaded the library source code and added a "throw" statement in the catch region of the PerformWebRequest method, and this was the result:

Unhandled Exception: System.Net.WebException: Error getting response stream (Trust failure): TrustFailure ---> System.IO.IOException: The authentication or decryption has failed. ---> Mono.Security.Protocol.Tls.TlsException: Invalid certificate received from server.
  at Mono.Security.Protocol.Tls.Handshake.Client.TlsServerCertificate.validateCertificates (Mono.Security.X509.X509CertificateCollection certificates) [0x00000] in <filename unknown>:0
  at Mono.Security.Protocol.Tls.Handshake.Client.TlsServerCertificate.ProcessAsTls1 () [0x00000] in <filename unknown>:0
  at Mono.Security.Protocol.Tls.Handshake.HandshakeMessage.Process () [0x00000] in <filename unknown>:0
  at (wrapper remoting-invoke-with-check) Mono.Security.Protocol.Tls.Handshake.HandshakeMessage:Process ()
  at Mono.Security.Protocol.Tls.ClientRecordProtocol.ProcessHandshakeMessage (Mono.Security.Protocol.Tls.TlsStream handMsg) [0x00000] in <filename unknown>:0
  at Mono.Security.Protocol.Tls.RecordProtocol.InternalReceiveRecordCallback (IAsyncResult asyncResult) [0x00000] in <filename unknown>:0
  --- End of inner exception stack trace ---
  at Mono.Security.Protocol.Tls.SslStreamBase.AsyncHandshakeCallback (IAsyncResult asyncResult) [0x00000] in <filename unknown>:0
  --- End of inner exception stack trace ---
  at System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult) [0x00000] in <filename unknown>:0
  at System.Net.HttpWebRequest.GetResponse () [0x00000] in <filename unknown>:0
  at AppLimit.CloudComputing.OAuth.OAuthStreamHelper.GetResponse (System.Net.WebRequest request) [0x00000] in <filename unknown>:0
  at AppLimit.CloudComputing.OAuth.OAuthService.PerformWebRequest (System.String url, System.String method, System.Collections.Generic.Dictionary`2 multiFormDataStream) [0x00000] in <filename unknown>:0

So the NullReferenceException was caused by the "retStatus = resp.StatusCode;" statement I think.
Anyway I found that the problem was the lack of root certificates in Mono (which wasn't handled by the library, see this link) and thus resulting in an exception.
Under Linux, I solved by running:

mozroots --import --machine

and confirming about 100 times with "yes".
And now the C# app works! If you want, you can add a tip under the Documentation section of this "issue" as it could help others.
Or, better, in the next version you can handle the WebExceptionStatus.TrustFailure and implementing ICertificatePolicy to always accept the certificate.

Coordinator
Jan 13, 2011 at 9:28 PM
Hi,

thanks a lot for this information. We will add support in the library for this kind of errors.

Dirk

2011/1/12 virtualdj <notifications@codeplex.com>

From: virtualdj

Hi, I was doing more testing on the app to publish the issue and unexpectly I have found the solution!
The exception thrown was:

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object
at AppLimit.CloudComputing.OAuth.OAuthService.PerformWebRequest (System.String url, System.String method, System.Collections.Generic.Dictionary`2 multiFormDataStream) [0x00000] in <filename unknown>:0
at AppLimit.CloudComputing.OAuth.OAuthService.PerformTokenRequest (System.String requestTokenUrl) [0x00000] in <filename unknown>:0
at AppLimit.CloudComputing.OAuth.OAuthService.GetRequestToken (AppLimit.CloudComputing.OAuth.Context.OAuthServiceContext svcContext, AppLimit.CloudComputing.OAuth.Context.OAuthConsumerContext conContext) [0x00000] in <filename unknown>:0
at AppLimit.CloudComputing.SharpBox.DropBox.Logic.DropBoxApplication.AuthorizeAndGetSession (System.String userName, System.String password) [0x00000] in <filename unknown>:0
at AppLimit.CloudComputing.SharpBox.DropBox.Logic.DropBoxApplication.Authorize (System.String userName, System.String password) [0x00000] in <filename unknown>:0
at AppLimit.CloudComputing.SharpBox.DropBox.DropBoxStorageProvider.Open (ICloudStorageConfiguration configuration, ICloudStorageCredentials credentials) [0x00000] in <filename unknown>:0
at AppLimit.CloudComputing.SharpBox.CloudStorage.Open (ICloudStorageConfiguration configuration, ICloudStorageCredentials credentials) [0x00000] in <filename unknown>:0
at DropBoxUploadTest.Program.Main (System.String[] args) [0x00000] in <filename unknown>:0

It wasn't so useful, so I downloaded the library source code and added a "throw" statement in the catch region of the PerformWebRequest method, and this was the result:

Unhandled Exception: System.Net.WebException: Error getting response stream (Trust failure): TrustFailure ---> System.IO.IOException: The authentication or decryption has failed. ---> Mono.Security.Protocol.Tls.TlsException: Invalid certificate received from server.
at Mono.Security.Protocol.Tls.Handshake.Client.TlsServerCertificate.validateCertificates (Mono.Security.X509.X509CertificateCollection certificates) [0x00000] in <filename unknown>:0
at Mono.Security.Protocol.Tls.Handshake.Client.TlsServerCertificate.ProcessAsTls1 () [0x00000] in <filename unknown>:0
at Mono.Security.Protocol.Tls.Handshake.HandshakeMessage.Process () [0x00000] in <filename unknown>:0
at (wrapper remoting-invoke-with-check) Mono.Security.Protocol.Tls.Handshake.HandshakeMessage:Process ()
at Mono.Security.Protocol.Tls.ClientRecordProtocol.ProcessHandshakeMessage (Mono.Security.Protocol.Tls.TlsStream handMsg) [0x00000] in <filename unknown>:0
at Mono.Security.Protocol.Tls.RecordProtocol.InternalReceiveRecordCallback (IAsyncResult asyncResult) [0x00000] in <filename unknown>:0
--- End of inner exception stack trace ---
at Mono.Security.Protocol.Tls.SslStreamBase.AsyncHandshakeCallback (IAsyncResult asyncResult) [0x00000] in <filename unknown>:0
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult) [0x00000] in <filename unknown>:0
at System.Net.HttpWebRequest.GetResponse () [0x00000] in <filename unknown>:0
at AppLimit.CloudComputing.OAuth.OAuthStreamHelper.GetResponse (System.Net.WebRequest request) [0x00000] in <filename unknown>:0

at AppLimit.CloudComputing.OAuth.OAuthService.PerformWebRequest (System.String url, System.String method, System.Collections.Generic.Dictionary`2 multiFormDataStream) [0x00000] in <filename unknown>:0

So the NullReferenceException was caused by the "retStatus = resp.StatusCode;" statement I think.
Anyway I found that the problem was the lack of root certificates in Mono (which wasn't handled by the library, see this link) and thus resulting in an exception.
Under Linux, I solved by running:

mozroots --import --machine

and confirming about 100 times with "yes".
And now the C# app works! If you want, you can add a tip under the Documentation section of this "issue" as it could help others.
Or, better, in the next version you can handle the WebExceptionStatus.TrustFailure and implementing ICertificatePolicy to always accept the certificate.

Read the full discussion online.

To add a post to this discussion, reply to this email (sharpbox@discussions.codeplex.com)

To start a new discussion for this project, email sharpbox@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com


Coordinator
Jan 13, 2011 at 9:31 PM

Hi,

I added an issue for this: http://sharpbox.codeplex.com/workitem/15095

Thanks again
Dirk 

Jan 14, 2011 at 7:28 PM

Hi, I'm glad that the provided info was helpful.

Initially I was thinking that I was a totally newbie for not being able to make such a simple program to work. But, at least the "certificate issue" of Mono wasn't simple to find.

 

Anyway, good job and thank you too.