in OAuthWebSample/OAuthWebSample/Controllers/OAuthController.cs [159:203]
public async Task<ActionResult> RefreshToken(string refreshToken)
{
String error = null;
if (!String.IsNullOrEmpty(refreshToken))
{
// Form the request to exchange an auth code for an access token and refresh token
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, ConfigurationManager.AppSettings["TokenUrl"]);
requestMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
Dictionary<String, String> form = new Dictionary<String, String>()
{
{ "client_assertion_type", "urn:ietf:params:oauth:client-assertion-type:jwt-bearer" },
{ "client_assertion", ConfigurationManager.AppSettings["ClientAppSecret"] },
{ "grant_type", "refresh_token" },
{ "assertion", refreshToken },
{ "redirect_uri", ConfigurationManager.AppSettings["CallbackUrl"] }
};
requestMessage.Content = new FormUrlEncodedContent(form);
// Make the request to exchange the auth code for an access token (and refresh token)
HttpResponseMessage responseMessage = await s_httpClient.SendAsync(requestMessage);
if (responseMessage.IsSuccessStatusCode)
{
// Handle successful request
String body = await responseMessage.Content.ReadAsStringAsync();
ViewBag.Token = JObject.Parse(body).ToObject<TokenModel>();
}
else
{
error = responseMessage.ReasonPhrase;
}
}
else
{
error = "Invalid refresh token";
}
if (!String.IsNullOrEmpty(error))
{
ViewBag.Error = error;
}
return View("TokenView");
}