首页 > 解决方案 > Identitty4 Access Token from Android and token lenght

问题描述

I have a console application where I use to test some code. In my console app everything works as expected. Same code implemented in the xamarin forms and I get an un tI sent a request to identity4 to get a token and it works receiving a token 685 characters of length. The same code that I use in the console is implemented in android on a button click action and I receive the token as well. I try to use the access token to my protected api and I get Unauthorized. I try to use the token in postman and I get Unauthorized. If I use the token received in my console in the postman it works! What I realize is that in the console app the access token have a length of 865 characters and in android the access token have a length of 864. I checked the length from the content which I receive before apply NewtonSoft Json converter

My console code:

private static async Task Main()
    {

        Console.WriteLine("Request For a token");

        //var handler = new HttpClientHandler();
        //handler.ClientCertificateOptions = ClientCertificateOption.Manual;
        //handler.SslProtocols = SslProtocols.Tls12;
        //handler.ClientCertificates.Add(new X509Certificate2("rsaCert.pfx","1234"));
        //var client = new HttpClient(handler);

        // discover endpoints from metadata
        var client = new HttpClient();

        //client.BaseAddress = new Uri("http://localhost:5000");
        var content = new FormUrlEncodedContent(new[]
        {
            new KeyValuePair<string, string>("grant_type", "password"),
            new KeyValuePair<string, string>("username", "test"),
            new KeyValuePair<string, string>("password", "test"),
            new KeyValuePair<string, string>("scope", "catalogapi1"),
            new KeyValuePair<string, string>("client_id", "ro.client"),
            new KeyValuePair<string, string>("client_secret", "secret")
        });

        var result = await client.PostAsync("http://localhost:5000/connect/token", content);
        string resultContent = await result.Content.ReadAsStringAsync();
        Console.WriteLine(resultContent);

        ServerReponse responseToken = JsonConvert.DeserializeObject<ServerReponse>(resultContent);
        Console.WriteLine("Token Lenght: "+ responseToken.access_token.Length);
        Console.WriteLine("\n\n");

        // call api
        var apiClient = new HttpClient();
        apiClient.SetBearerToken(responseToken.access_token);

        var response = await apiClient.GetAsync("http://localhost:5100/api/v1/products");
        if (!response.IsSuccessStatusCode)
        {
            Console.WriteLine(response.StatusCode);
        }
        else
        {
            var content2 = await response.Content.ReadAsStringAsync();
            Console.WriteLine(JArray.Parse(content2));
        }

        Console.WriteLine("Press a key to close the application");
        Console.ReadLine();

    }

And in my Android event click action:

async void OnButtonClicked(object sender, EventArgs args)
    {
        var client = new HttpClient();

        //client.BaseAddress = new Uri("http://localhost:5000");
        var content = new FormUrlEncodedContent(new[]
        {
            new KeyValuePair<string, string>("grant_type", "password"),
            new KeyValuePair<string, string>("username", "test"),
            new KeyValuePair<string, string>("password", "test"),
            new KeyValuePair<string, string>("scope", "test"),
            new KeyValuePair<string, string>("client_id", "ro.client"),
            new KeyValuePair<string, string>("client_secret", "secret")
        });

        var result = await client.PostAsync("http://10.0.2.2:5000/connect/token", content);
        string resultContent = await result.Content.ReadAsStringAsync();
        Console.WriteLine(resultContent);

        ServerReponse responseToken = JsonConvert.DeserializeObject<ServerReponse>(resultContent);

        // call api
        var apiClient = new HttpClient();
        apiClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", responseToken.access_token.Trim());
        // apiClient.SetBearerToken(responseToken.access_token);

        var response = await apiClient.GetAsync("http://10.0.2.2:5100/api/v1/products");
        if (!response.IsSuccessStatusCode)
        {
            Console.WriteLine(response.StatusCode);
        }
        else
        {
            var content2 = await response.Content.ReadAsStringAsync();
            Console.WriteLine(JArray.Parse(content2));
        }

        Console.WriteLine("Press a key to close the application");
        Console.ReadLine();
    }

    public class ServerReponse
    {

        public string access_token { get; set; }
        public string expires_in { get; set; }
        public string token_type { get; set; }
        public string scope { get; set; }

    }

标签: xamarinxamarin.formsxamarin.android

解决方案


推荐阅读