OAuth Api call success
This commit is contained in:
@ -1,6 +1,9 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.Mvc;
|
||||
|
||||
@ -8,11 +11,35 @@ namespace testOauthClient.Controllers
|
||||
{
|
||||
public class HomeController : Controller
|
||||
{
|
||||
[HttpGet]
|
||||
public IActionResult Index()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> GetUserInfo(CancellationToken cancellationToken)
|
||||
{
|
||||
using (var client = new HttpClient()) {
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, "http://dev.pschneider.fr/api/me");
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken);
|
||||
|
||||
var response = await client.SendAsync(request, cancellationToken);
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
return View("Index", 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;
|
||||
}
|
||||
}
|
||||
public IActionResult About()
|
||||
{
|
||||
ViewData["Message"] = "Your application description page.";
|
||||
|
Reference in New Issue
Block a user