0

I'm beginner and I work one Xamarin app. I try to connect my app one Https server for get a Token, and when I test connection with Postman, all it's okay. But with my app, is a other story..

The web service is : aps.net-web-api-2

And this is my connection code in Xamarin:

using System.Net.Http;

protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        SetContentView(Resource.Layout.Main);
        Button button = FindViewById<Button>(Resource.Id.getButton);

        button.Click += async (sender, e) =>
        {
            try
            {
                string url = "https://urlexample/foo/bar/Token";
                string values = await RequestWeb(url);
            }
            catch (Exception ex)
            {
                Log.Info("error with click", ex.ToString());
            }
        };
    }

    private async Task<string> RequestHttps(string url)
    {
        string responseString = string.Empty;

        using (HttpClient client = new HttpClient())
        {
            Dictionary<string,string> values = new Dictionary<string, string>
            {
               { "grant_type", "password" },
               { "username", "User1" },
               { "password", "123456" }
            };

            FormUrlEncodedContent content = new FormUrlEncodedContent(values);
            HttpResponseMessage response = await client.PostAsync(url, content);

            responseString = await response.Content.ReadAsStringAsync();
        }
        return responseString;
    }

When I test with my code, I recovers my exception with an Try and Catch :

I/error with click(14476): System.Net.WebException: Error: TrustFailure (The authentication or decryption has failed.)

The probleme comes from this line :

HttpResponseMessage response = await client.PostAsync(url, content);

Example with Postman, for this connection, I get a connection Token :

Post -> Headers
- Content_type : application/x-www-form-urlencoded
- grand_type : password
- username : User1
- password : 123456

result :

{ "access_token": "ATT4U2xGQqsdf", "token_type": "bearer", "expires_in": 15 }

I try anothers ways for connection too (example here) And I wondering, when I read a cours for Android (here) it is said that do not create connection whit thread UI, but use another thread. But a lot of examples that I find don't use thread UI, why ? Or I misunderstood one thing ?

I also wonder if the probleme is from my settings ?

Or maybe I'm just totally lost... (haha).

Best regards, Romain

1 Answers1

2

Please use this two lines of code before InitializeComponent(); in your App.cs code.

System.Security.Cryptography.AesCryptoServiceProvider AES = new System.Security.Cryptography.AesCryptoServiceProvider();

System.Net.ServicePointManager.ServerCertificateValidationCallback += (o, certificate, chain, errors) => true;

By default Mono has no Certificates that are trusted, so you either have to skip the error or add a certificate that complements your POST.

Zroq
  • 8,002
  • 3
  • 26
  • 37