1

On load, my login page determines if the query param is null. If it is, it redirects to another page than then redirects back to login, but provides the query param. That looks like this:

protected void Page_Load(object sender, EventArgs e) {
    if (Request.Params["code"] == null) {
        var authCodeUrl = ConfigurationManager.AppSettings["MyId.AuthCodeUrl"];
        Response.Redirect(authCodeUrl);
    }
    else {
        var code = Request.Params["code"];
        GetAuthResult(code);
    }
}

GetAuthResult fires after the return redirect. In it, it sends a POST to a separate API and expects to get a return result:

using (var client = new HttpClient()) {
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));

    var response = client.PostAsync(accessTokenUrl, new FormUrlEncodedContent(bodyData)).Result;
    if (!response.IsSuccessStatusCode) {
        throw new Exception("MyID auth failed", new Exception(response.ReasonPhrase));
    }
    var token = response.Content.ReadAsStringAsync().Result;
    return JsonConvert.DeserializeObject<MyIdAuthResult>(token);
}

You can see it happen in the GIF below. When I step over line 43, I expect to check the result on line 44. Except Page_Load fires again. Any ideas what I'm doing wrong?

enter image description here

ernest
  • 1,633
  • 2
  • 30
  • 48
  • I'm not very familiar with `Async/Await`, but doesn't `PostAsync` need an `Await` somewhere? I don't know. And are you sure the `Post` is actually returning something? – wazz Oct 31 '21 at 17:16
  • @wazz If I recall correctly, `.Result` makes it synchronous. Doing `.GetAwaiter().GetResult()` produces the same result. – ernest Nov 01 '21 at 13:28
  • Any help [here](https://stackoverflow.com/questions/36625881/how-do-i-pass-an-object-to-httpclient-postasync-and-serialize-as-a-json-body/52857689)? Some newer answers at the bottom. – wazz Nov 01 '21 at 18:21

1 Answers1

0

It turns out an error was occurring, but since I didn't have a try catch, it appeared as if the page was just refreshing. The actual problem was that the endpoint I was hitting was HTTPS and the framework version was trying to use TLS 1.0. It's probably not the best solution, but I set the security protocol before instantiating the HttpClient and it worked:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
ernest
  • 1,633
  • 2
  • 30
  • 48