简体   繁体   中英

MVC accessing external Web API using login credentials

In need of some help accessing an external Web API passing along credentials in order to access the methods available. I have included the code below that i use in order to attempt to access the Web API. However, i receive the following error every time i attempt to access it:

"The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel."

What am i missing or what am i doing wrong? I have been circling around this for a couple days and have tried a couple different techniques but continue to get the same error. Here is one technique that i used.

 private static async Task<string> GetAPIToken(string userName, string password, string apiBaseUri)
    {
        try
        {
            using (var client = new HttpClient())
            {
                //setup client
                client.BaseAddress = new Uri(apiBaseUri);
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                //setup login data
                var formContent = new FormUrlEncodedContent(new[]
            {
                new KeyValuePair<string,string>("username",userName),
                new KeyValuePair<string,string>("password",password),
            });

                //send request
                HttpResponseMessage responseMessage = await client.PostAsync("Token", formContent);

                //get access token from response body
                var responseJson = await responseMessage.Content.ReadAsStringAsync();
                var jobject = JObject.Parse(responseJson);
                return jobject.GetValue("access_token").ToString();
            }
        }
        catch (Exception ex)
        {
            return null;
        }
    }

Any help would be greatly appreciated.

Thanks

There is a little bit of a difference when using HTTPS vs HTTP. This question should give you the information you need to fix your problem.

Make Https call using HttpClient

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM