|
|
|
@ -3,6 +3,7 @@ using System.Net.Http; |
|
|
|
using System.Net.Http.Headers; |
|
|
|
using System.Threading; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using Microsoft.AspNetCore.Authentication; |
|
|
|
using Microsoft.AspNetCore.Authorization; |
|
|
|
using Microsoft.AspNetCore.Mvc; |
|
|
|
|
|
|
|
@ -16,8 +17,14 @@ namespace Mvc.Client.Controllers { |
|
|
|
[Authorize, HttpPost("~/")] |
|
|
|
public async Task<ActionResult> Index(CancellationToken cancellationToken) { |
|
|
|
using (var client = new HttpClient()) { |
|
|
|
var token = await HttpContext.Authentication.GetTokenAsync("access_token"); |
|
|
|
if (string.IsNullOrEmpty(token)) { |
|
|
|
throw new InvalidOperationException("The access token cannot be found in the authentication ticket. " + |
|
|
|
"Make sure that SaveTokens is set to true in the OIDC options."); |
|
|
|
} |
|
|
|
|
|
|
|
var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost:54540/api/message"); |
|
|
|
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken); |
|
|
|
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token); |
|
|
|
|
|
|
|
var response = await client.SendAsync(request, cancellationToken); |
|
|
|
response.EnsureSuccessStatusCode(); |
|
|
|
@ -25,16 +32,5 @@ namespace Mvc.Client.Controllers { |
|
|
|
return View("Home", model: await response.Content.ReadAsStringAsync()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
protected string AccessToken { |
|
|
|
get { |
|
|
|
var claim = HttpContext.User?.FindFirst("access_token"); |
|
|
|
if (claim == null) { |
|
|
|
throw new InvalidOperationException(); |
|
|
|
} |
|
|
|
|
|
|
|
return claim.Value; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |