GetJson helper

main
Paul Schneider 9 months ago
parent db9994b05a
commit 204bb80252
4 changed files with 33 additions and 49 deletions

@ -1,37 +1,15 @@
using System;
using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using IdentityModel.Client;
using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging; using Yavsc.WebApiClient.Helpers;
using Newtonsoft.Json.Linq;
namespace testOauthClient.Controllers namespace sampleWebAsWebApiClient.Controllers
{ {
public class HomeController : Controller public class HomeController(ILoggerFactory loggerFactory) : Controller
{ {
readonly ILogger _logger; readonly ILogger _logger = loggerFactory.CreateLogger<HomeController>();
public class GCMRegistrationRecord
{
public string GCMRegistrationId { get; set; } = "testGoogleRegistrationIdValue";
public string DeviceId { get; set; } = "TestDeviceId";
public string Model { get; set; } = "TestModel";
public string Platform { get; set; } = "External Web";
public string Version { get; set; } = "0.0.1-rc1";
}
public HomeController(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<HomeController>();
}
[HttpGet] [HttpGet]
public IActionResult Index() public IActionResult Index()
{ {
@ -49,36 +27,22 @@ namespace testOauthClient.Controllers
ViewBag.Json = content; ViewBag.Json = content;
return View("json"); return View("json");
} }
[HttpPost] [HttpPost]
public async Task<IActionResult> GetUserInfo(CancellationToken cancellationToken) public async Task<IActionResult> GetUserInfo(CancellationToken cancellationToken)
{ {
var accessToken = await HttpContext.GetTokenAsync("access_token"); var accessToken = await HttpContext.GetTokenAsync("access_token");
var client = new HttpClient(new HttpClientHandler(){ AllowAutoRedirect=false });
client.DefaultRequestHeaders.Add("Accept", "application/json"); string json = await HttpContext.GetJson("https://localhost:6001/api/account/me");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); return View("UserInfo", json);
var content = await client.GetAsync("https://localhost:6001/api/account/me");
content.EnsureSuccessStatusCode();
var json = await content.Content.ReadAsStreamAsync();
var obj = JsonSerializer.Deserialize<JsonElement>(json);
return View("UserInfo", obj.ToString());
} }
[HttpPost] [HttpPost]
public async Task<IActionResult> GetApiCall(CancellationToken cancellationToken) public async Task<IActionResult> GetIdentity(CancellationToken cancellationToken)
{ {
var accessToken = await HttpContext.GetTokenAsync("access_token"); string json = await HttpContext.GetJson("https://localhost:6001/identity");
var client = new HttpClient(new HttpClientHandler(){ AllowAutoRedirect=false });
client.DefaultRequestHeaders.Add("Accept", "application/json");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
var content = await client.GetAsync("https://localhost:6001/identity");
content.EnsureSuccessStatusCode();
var json = await content.Content.ReadAsStringAsync();
return View("UserInfo", json); return View("UserInfo", json);
} }
public IActionResult About() public IActionResult About()
{ {
ViewData["Message"] = "Your application description page."; ViewData["Message"] = "Your application description page.";

@ -0,0 +1,20 @@
using System.Net.Http.Headers;
using Microsoft.AspNetCore.Authentication;
namespace Yavsc.WebApiClient.Helpers;
public static class HttpContextHelpers
{
public static async Task<string> GetJson(this HttpContext httpContext, string endPoint)
{
var accessToken = await httpContext.GetTokenAsync("access_token");
var client = new HttpClient(new HttpClientHandler() { AllowAutoRedirect = false });
client.DefaultRequestHeaders.Add("Accept", "application/json");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
var content = await client.GetAsync(endPoint);
content.EnsureSuccessStatusCode();
var json = await content.Content.ReadAsStringAsync();
return json;
}
}

@ -3,7 +3,7 @@
<head> <head>
<meta charset="utf-8" /> <meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - testOauthClient</title> <title>@ViewData["Title"] - sampleWebAsWebApiClient</title>
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" /> <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" />
<link rel="stylesheet" href="~/css/site.css" /> <link rel="stylesheet" href="~/css/site.css" />
@ -18,7 +18,7 @@
<span class="icon-bar"></span> <span class="icon-bar"></span>
<span class="icon-bar"></span> <span class="icon-bar"></span>
</button> </button>
<a asp-controller="Home" asp-action="Index" class="navbar-brand">testOauthClient</a> <a asp-controller="Home" asp-action="Index" class="navbar-brand">sampleWebAsWebApiClient</a>
</div> </div>
<div class="navbar-collapse collapse"> <div class="navbar-collapse collapse">
<ul class="nav navbar-nav"> <ul class="nav navbar-nav">
@ -33,7 +33,7 @@
@RenderBody() @RenderBody()
<hr /> <hr />
<footer> <footer>
<p>&copy; 2016 - testOauthClient</p> <p>&copy; 2016 - sampleWebAsWebApiClient</p>
</footer> </footer>
</div> </div>

@ -1,2 +1,2 @@
@using testOauthClient @using sampleWebAsWebApiClient
@addTagHelper "*, Microsoft.AspNet.Mvc.TagHelpers" @addTagHelper "*, Microsoft.AspNet.Mvc.TagHelpers"

Loading…