Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
244 views
in Technique[技术] by (71.8m points)

c# - Xamarin forms aysnc issue with authentication method

I am trying to authenticate my http client with the barrer token inside a xamrian forms app but am having a issue with async At the min what is hapening is it goes into the authenticate user method fine but it doesnt complete the code until later on in the get all add ons function.

private async void AuthenticateUser() {
     List result = new List();
     client.AuthenticateUser();
     result = await client.GetAllAddons();

}

Here is my authenticate method For Completeness I will show my class constructur where the http client gets defined.

public MSFSAddonClient() {
        client = new HttpClient();
        currentToken = new JWTToken();
        client.BaseAddress = new Uri(ApiUrl);
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}

Here is the authentication method the problem comes at this point it skips the check that the call sucessed and then goes into my actual get add ons call.

var httpResponse = await client.PostAsync(stringPayload, httpContent);
        

public async void AuthenticateUser()
    {
        var requestUri = string.Format(ApiUrl + Authenticate);

        AuthenticateRequest myuser = new AuthenticateRequest();
        myuser.Username = "secret";
        myuser.Password = "secret";
        JWTToken _result = new JWTToken();
        string stringPayload = JsonConvert.SerializeObject(myuser);
        var httpContent = new StringContent(stringPayload, Encoding.UTF8, "application/json");

        // Do the actual request and await the response
        var httpResponse = await client.PostAsync(stringPayload, httpContent);
        if (httpResponse.IsSuccessStatusCode)
        {
            var responseContent = await httpResponse.Content.ReadAsStringAsync();
            _result = JsonConvert.DeserializeObject(responseContent);
        }

        currentToken = _result;
}

This is the method this should only get called after the above method is complete

public async Task> GetAllAddons() {
        string requestUri = string.Format(GetAllAddonsEndPOint);
        List _result = new List();

        HttpResponseMessage responseMessage = await client.PostAsync(requestUri, baseContent);
        client.DefaultRequestHeaders.Authorization =
            new AuthenticationHeaderValue("Bearer", currentToken.jwtToken);

        if (responseMessage.IsSuccessStatusCode)
        {
            var byteArray = await responseMessage.Content.ReadAsByteArrayAsync();
            var content = Encoding.UTF8.GetString(byteArray, 0, byteArray.Length);
            _result = JsonConvert.DeserializeObject>(content);
        }
        return _result;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...