GetJson helper
This commit is contained in:
@ -1,37 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
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.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Yavsc.WebApiClient.Helpers;
|
||||
|
||||
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]
|
||||
public IActionResult Index()
|
||||
{
|
||||
@ -49,35 +27,21 @@ namespace testOauthClient.Controllers
|
||||
ViewBag.Json = content;
|
||||
return View("json");
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> GetUserInfo(CancellationToken cancellationToken)
|
||||
{
|
||||
|
||||
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("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]
|
||||
public async Task<IActionResult> GetApiCall(CancellationToken cancellationToken)
|
||||
{
|
||||
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("https://localhost:6001/identity");
|
||||
content.EnsureSuccessStatusCode();
|
||||
var json = await content.Content.ReadAsStringAsync();
|
||||
string json = await HttpContext.GetJson("https://localhost:6001/api/account/me");
|
||||
return View("UserInfo", json);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> GetIdentity(CancellationToken cancellationToken)
|
||||
{
|
||||
string json = await HttpContext.GetJson("https://localhost:6001/identity");
|
||||
return View("UserInfo", json);
|
||||
}
|
||||
|
||||
public IActionResult About()
|
||||
{
|
||||
|
20
src/sampleWebAsWebApiClient/Helpers/HttpContextHelpers.cs
Normal file
20
src/sampleWebAsWebApiClient/Helpers/HttpContextHelpers.cs
Normal file
@ -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>
|
||||
<meta charset="utf-8" />
|
||||
<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="~/css/site.css" />
|
||||
@ -18,7 +18,7 @@
|
||||
<span class="icon-bar"></span>
|
||||
<span class="icon-bar"></span>
|
||||
</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 class="navbar-collapse collapse">
|
||||
<ul class="nav navbar-nav">
|
||||
@ -33,7 +33,7 @@
|
||||
@RenderBody()
|
||||
<hr />
|
||||
<footer>
|
||||
<p>© 2016 - testOauthClient</p>
|
||||
<p>© 2016 - sampleWebAsWebApiClient</p>
|
||||
</footer>
|
||||
</div>
|
||||
|
||||
|
@ -1,2 +1,2 @@
|
||||
@using testOauthClient
|
||||
@using sampleWebAsWebApiClient
|
||||
@addTagHelper "*, Microsoft.AspNet.Mvc.TagHelpers"
|
||||
|
Reference in New Issue
Block a user