Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
140 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<AddonsViewModel> result = new List<AddonsViewModel>();
     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<JWTToken>(responseContent);
        }

        currentToken = _result;
}

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

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

        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<List<AddonsViewModel>>(content);
        }
        return _result;
}

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

1 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...