forked from DuendeArchive/IdentityModel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHomeController.cs
More file actions
71 lines (57 loc) · 2.08 KB
/
HomeController.cs
File metadata and controls
71 lines (57 loc) · 2.08 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System.Net.Http;
using System.Threading.Tasks;
using IdentityModel.Client;
using Microsoft.AspNetCore.Mvc;
namespace WebApplication1.Controllers
{
public class HomeController : Controller
{
public HomeController(IHttpClientFactory httpClientFactory)
{
HttpClientFactory = httpClientFactory;
}
public IHttpClientFactory HttpClientFactory { get; }
public TokenClient TokenClient { get; }
public IActionResult Index()
{
return View();
}
public async Task<string> NoFactory()
{
var client = new HttpClient();
var response = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
{
Address = "https://demo.identityserver.io/connect/token",
ClientId = "client",
ClientSecret = "secret"
});
return response.AccessToken ?? response.Error;
}
public async Task<string> Simple()
{
var client = HttpClientFactory.CreateClient();
var response = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
{
Address = "https://demo.identityserver.io/connect/token",
ClientId = "client",
ClientSecret = "secret"
});
return response.AccessToken ?? response.Error;
}
public async Task<string> WithAddress()
{
var client = HttpClientFactory.CreateClient("token_client");
var response = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
{
ClientId = "client",
ClientSecret = "secret"
});
return response.AccessToken ?? response.Error;
}
public async Task<string> Typed([FromServices] TokenClient tokenClient)
{
var response = await tokenClient.RequestClientCredentialsTokenAsync();
return response.AccessToken;
}
}
}