Below is the code which works perfectly fine when i execute it on console application.
The line
var postResponse = await client.SendAsync(req); give the result when i run the code in console app.
But when iam using this code in WebApi controller, this code halts on this line.
using (var client = new HttpClient())
{
var auth = "MTAwNDgucnVsZXNlbmdpbmUuc2VydmljZTp2N3FuY3I4cWlz";
client.BaseAddress = new Uri("https://federation-sts-stage.accenture.com");
var req = new HttpRequestMessage(HttpMethod.Post, "https://federation-sts-stage.test.com/oauth/ls/connect/token");
var cont = new FormUrlEncodedContent(bodyContents);
cont.Headers.ContentType = new MediaTypeHeaderValue("application/json");
cont.Headers.ContentLength = Convert.ToInt64("125");
req.Content = cont;
req.Headers.Add("Authorization", "Basic " + auth);
try
{
var postResponse = await client.SendAsync(req); // this is where the code keeps on waiting but works fine in console app
postResponse.EnsureSuccessStatusCode();
responseContents = postResponse.Content.ReadAsStringAsync().Result;
}
catch (Exception ex)
{
var msg = ex.Message;
return msg;
}
var responseObject = JObject.Parse(responseContents);
return responseObject.Value<string>("access_token");
}
I have also compared the request object in both the cases (in console app and in webapi controller call) but in both the cases the request object comes out same as below :
{Method: POST, RequestUri: 'https://federation-sts-stage.test.com/oauth/ls/connect/token', Version: 1.1, Content: System.Net.Http.FormUrlEncodedContent, Headers:
{
Authorization: Basic MTAwNDgucnVsZXNlbmdpbmUuc2VydmljZTp2N3FuY3I4cWlz
Content-Type: application/json
Content-Length: 125
}}
I dont know what iam doing incorrect.
As per the comments, i am putting the whole method which gets called from apicontroller as below, this method works fine from console app but when i call this method from apicontroller its kept on running.
public async Task<string> RequestTokenFromIssuer(string username, string password)
{
var bodyContents = new Dictionary<string, string>
{
{ "grant_type", "client_credentials" },
{ "userName", username },
{ "password", password},
{ "scope", "read_rulesengine write_rulesengine" }
};
string responseContents = string.Empty;
using (var client = new HttpClient())
{
var auth = "MTAwNDgucnVsZXNlbmdpbmUuc2VydmljZTp2N3FuY3I4cWlz";
client.BaseAddress = new Uri("https://federation-sts-stage.test.com");
var req = new HttpRequestMessage(HttpMethod.Post, "https://federation-sts-stage.test.com/oauth/ls/connect/token");
var cont = new FormUrlEncodedContent(bodyContents);
cont.Headers.ContentType = new MediaTypeHeaderValue("application/json");
cont.Headers.ContentLength = Convert.ToInt64("125");
req.Content = cont;
req.Headers.Add("Authorization", "Basic " + auth);
try
{
var postResponse = await client.SendAsync(req);
postResponse.EnsureSuccessStatusCode();
responseContents = postResponse.Content.ReadAsStringAsync().Result;
}
catch (Exception ex)
{
var msg = ex.Message;
return msg;
}
var responseObject = JObject.Parse(responseContents);
return responseObject.Value<string>("access_token");
}
}