/
HomeController.cs
46 lines (41 loc) · 1.3 KB
/
HomeController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using IdentityServerWebSite.Models;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace IdentityServerWebSite.Controllers
{
[Route("")]
public class HomeController : Controller
{
[HttpGet("")]
public IActionResult Index()
{
return View();
}
[HttpGet("private")]
[Authorize]
public async Task<IActionResult> Private()
{
var model = new TimeViewModel
{
Time = await GetTimeAsync()
};
return View(model);
}
public async Task<string> GetTimeAsync()
{
using (var httpClient = new HttpClient())
{
string accessToken = await HttpContext.Authentication.GetTokenAsync("access_token");
httpClient.BaseAddress = new Uri("http://localhost:5001");
httpClient.DefaultRequestHeaders.Clear();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
return await httpClient.GetStringAsync("api/clock/time");
}
}
}
}