public async Task <ActionResult> Authenticate(string code, string state) { var baseTokenUrl = ConfigurationManager.AppSettings["BaseTokenUrl"]; var authenticateUrl = $"{baseTokenUrl}"; var clientId = ConfigurationManager.AppSettings["ClientId"]; var payload = new Dictionary <string, string>() { { "client_id", clientId }, { "client_secret", ConfigurationManager.AppSettings["ClientSecret"] }, { "code", code }, { "grant_type", ConfigurationManager.AppSettings["GrantType"] }, { "redirect_uri", ConfigurationManager.AppSettings["RedirectUri"] }, { "state", state } }; var content = new FormUrlEncodedContent(payload); var client = new HttpClient(); client.DefaultRequestHeaders.Add("accept", "application/json"); var response = await client.PostAsync(authenticateUrl, content); if (response.IsSuccessStatusCode) { var json = await response.Content.ReadAsStringAsync(); var authenticationResponse = JsonConvert.DeserializeObject <AuthenticationResponse>(json); var accessTokenManager = new AccessTokenManager(); accessTokenManager.saveAccessToken(authenticationResponse.access_token); var referringActionManager = new ReferringActionManager(); var referringAction = referringActionManager.getReferringAction(); return(await Task.Run(() => Redirect(referringAction))); } return(await Task.Run(() => RedirectToAction("Index"))); }