OAuth Api call success

This commit is contained in:
2016-06-13 13:33:32 +02:00
parent 8ce7767672
commit ca4625a7cf
22 changed files with 1510 additions and 1779 deletions

View File

@ -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.";