I have the following methods in the Global.asax of my MVC app:
protected void Application_OnPostAuthenticateRequest(object sender, EventArgs e)
{
SetUserRolesToSession().GetAwaiter().GetResult();
}
protected async Task SetUserRolesToSession()
{
var networkUserIdParts = networkUserId.Split(new char[] { '\\' });
var baseUrl = ConfigurationManager.AppSettings["SecurityApiBaseUrl"].ToString();
var userSearchRequest = new UserSearchRequest
{
NetworkUserId = Request.LogonUserIdentity.Name
};
var user = (await new SecurityApiServiceWrapper(baseUrl)
.GetUsersAsync(userSearchRequest)).FirstOrDefault();
Session["Roles" + "_" + networkUserId] = user?.Groups.Select(x => x.Name).ToList();
}
Then I have the following code in SecurityApiServiceWrapper:
public async Task<List<User>> GetUsersAsync(UserSearchRequest userSearchRequest)
{
using (var client = GetHttpClient(_baseUrl))
{
var response = await client.PostAsJsonAsync("api/v1/users/search", userSearchRequest);
if (response.IsSuccessStatusCode)
{
var users = await response.Content.ReadAsAsync<List<User>>();
return users;
}
else //not 200
{
}
}
}
public HttpClient GetHttpClient(string baseUrl)
{
var client = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true });
client.BaseAddress = new Uri(baseUrl);
client.Timeout = Timeout.InfiniteTimeSpan;
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
return client;
}
When the code steps into await client.PostAsJsonAsync, the process gets stuck and never returns. I can make the same api call through Postman and the response returns in < 1s. Any idea what the issue might be or how to debug?