Skip to content

Commit 2078b69

Browse files
committed
Move deriving OAuth client options from OpenId connect authentication scheme to separate library.
1 parent 6d70347 commit 2078b69

File tree

89 files changed

+74757
-141
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+74757
-141
lines changed

AppCore.Extensions.sln

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkerService", "Http\sampl
6161
EndProject
6262
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AppCore.Extensions.Http.Authentication.OAuth.AspNetCore", "Http\src\AppCore.Extensions.Http.Authentication.OAuth.AspNetCore\AppCore.Extensions.Http.Authentication.OAuth.AspNetCore.csproj", "{9301436D-85F6-4845-AAD6-33103F49F018}"
6363
EndProject
64+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AppCore.Extensions.Http.Authentication.OAuth.AspNetCore.OpenIdConnect", "Http\src\AppCore.Extensions.Http.Authentication.OAuth.AspNetCore.OpenIdConnect\AppCore.Extensions.Http.Authentication.OAuth.AspNetCore.OpenIdConnect.csproj", "{91754F75-CC7A-48C8-A133-9F087BE10CFE}"
65+
EndProject
66+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Web", "Http\samples\Web\Web.csproj", "{7D72ABA6-5EBE-491A-A599-65D15FD10308}"
67+
EndProject
6468
Global
6569
GlobalSection(SolutionConfigurationPlatforms) = preSolution
6670
Debug|Any CPU = Debug|Any CPU
@@ -143,6 +147,14 @@ Global
143147
{9301436D-85F6-4845-AAD6-33103F49F018}.Debug|Any CPU.Build.0 = Debug|Any CPU
144148
{9301436D-85F6-4845-AAD6-33103F49F018}.Release|Any CPU.ActiveCfg = Release|Any CPU
145149
{9301436D-85F6-4845-AAD6-33103F49F018}.Release|Any CPU.Build.0 = Release|Any CPU
150+
{91754F75-CC7A-48C8-A133-9F087BE10CFE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
151+
{91754F75-CC7A-48C8-A133-9F087BE10CFE}.Debug|Any CPU.Build.0 = Debug|Any CPU
152+
{91754F75-CC7A-48C8-A133-9F087BE10CFE}.Release|Any CPU.ActiveCfg = Release|Any CPU
153+
{91754F75-CC7A-48C8-A133-9F087BE10CFE}.Release|Any CPU.Build.0 = Release|Any CPU
154+
{7D72ABA6-5EBE-491A-A599-65D15FD10308}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
155+
{7D72ABA6-5EBE-491A-A599-65D15FD10308}.Debug|Any CPU.Build.0 = Debug|Any CPU
156+
{7D72ABA6-5EBE-491A-A599-65D15FD10308}.Release|Any CPU.ActiveCfg = Release|Any CPU
157+
{7D72ABA6-5EBE-491A-A599-65D15FD10308}.Release|Any CPU.Build.0 = Release|Any CPU
146158
EndGlobalSection
147159
GlobalSection(SolutionProperties) = preSolution
148160
HideSolutionNode = FALSE
@@ -177,5 +189,7 @@ Global
177189
{B093F9C1-5054-4198-AE1D-38510FA0547A} = {9416DF99-18D3-4373-A517-00A65E7CC32A}
178190
{FCE07DAA-08DC-41E0-B091-4A24F1538551} = {B093F9C1-5054-4198-AE1D-38510FA0547A}
179191
{9301436D-85F6-4845-AAD6-33103F49F018} = {B2F9DC67-6D7B-4070-8580-F401050445A9}
192+
{91754F75-CC7A-48C8-A133-9F087BE10CFE} = {B2F9DC67-6D7B-4070-8580-F401050445A9}
193+
{7D72ABA6-5EBE-491A-A599-65D15FD10308} = {B093F9C1-5054-4198-AE1D-38510FA0547A}
180194
EndGlobalSection
181195
EndGlobal
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System.Diagnostics;
2+
using System.Net.Http;
3+
using System.Text.Json.Nodes;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Authorization;
6+
using Microsoft.AspNetCore.Mvc;
7+
using Microsoft.Extensions.Logging;
8+
using Web.Models;
9+
10+
namespace Web.Controllers;
11+
12+
public class HomeController : Controller
13+
{
14+
private readonly IHttpClientFactory _httpClientFactory;
15+
private readonly ILogger<HomeController> _logger;
16+
17+
public HomeController(IHttpClientFactory httpClientFactory, ILogger<HomeController> logger)
18+
{
19+
_httpClientFactory = httpClientFactory;
20+
_logger = logger;
21+
}
22+
23+
public IActionResult Index()
24+
{
25+
return View();
26+
}
27+
28+
public IActionResult Privacy()
29+
{
30+
return View();
31+
}
32+
33+
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
34+
public IActionResult Error()
35+
{
36+
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
37+
}
38+
39+
[AllowAnonymous]
40+
public async Task<IActionResult> CallApiAsClient()
41+
{
42+
HttpClient client = _httpClientFactory.CreateClient("api-client");
43+
44+
string response = await client.GetStringAsync("test");
45+
ViewBag.Json = JsonNode.Parse(response)!.ToString();
46+
47+
return View("CallApi");
48+
}
49+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Web.Models;
2+
3+
public class ErrorViewModel
4+
{
5+
public string? RequestId { get; set; }
6+
7+
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
8+
}

Http/samples/Web/Program.cs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
using System;
2+
using Microsoft.AspNetCore.Authentication;
3+
using Microsoft.AspNetCore.Builder;
4+
using Microsoft.Extensions.DependencyInjection;
5+
using Microsoft.Extensions.Hosting;
6+
using Microsoft.IdentityModel.Tokens;
7+
8+
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
9+
10+
// Add services to the container.
11+
builder.Services.AddControllersWithViews();
12+
builder.Services
13+
.AddAuthentication(
14+
options =>
15+
{
16+
options.DefaultScheme = "cookie";
17+
options.DefaultChallengeScheme = "oidc";
18+
})
19+
.AddCookie(
20+
"cookie",
21+
options =>
22+
{
23+
options.Cookie.Name = "mvccode";
24+
25+
options.Events.OnSigningOut = async e =>
26+
{
27+
//await e.HttpContext.RevokeUserRefreshTokenAsync();
28+
};
29+
})
30+
.AddOpenIdConnect(
31+
"oidc",
32+
options =>
33+
{
34+
options.Authority = "https://demo.duendesoftware.com";
35+
36+
options.ClientId = "interactive.confidential.short";
37+
options.ClientSecret = "secret";
38+
39+
// code flow + PKCE (PKCE is turned on by default)
40+
options.ResponseType = "code";
41+
options.UsePkce = true;
42+
43+
options.Scope.Clear();
44+
options.Scope.Add("openid");
45+
options.Scope.Add("profile");
46+
options.Scope.Add("email");
47+
options.Scope.Add("offline_access");
48+
options.Scope.Add("api");
49+
50+
// not mapped by default
51+
options.ClaimActions.MapJsonKey("website", "website");
52+
53+
// keeps id_token smaller
54+
options.GetClaimsFromUserInfoEndpoint = true;
55+
options.SaveTokens = true;
56+
57+
options.TokenValidationParameters = new TokenValidationParameters
58+
{
59+
NameClaimType = "name",
60+
RoleClaimType = "role"
61+
};
62+
});
63+
64+
// add OAuth HTTP client authentication for OpenID connect scheme
65+
builder.Services
66+
.AddHttpClientAuthentication()
67+
.AddOAuthClientForScheme(
68+
c => c.OpenIdConnect(
69+
o =>
70+
{
71+
o.Scope = "api";
72+
}));
73+
74+
// add HTTP client with OAuth client authentication
75+
builder.Services
76+
.AddHttpClient(
77+
"api-client",
78+
client =>
79+
{
80+
client.BaseAddress = new Uri("https://demo.duendesoftware.com/api/");
81+
})
82+
.AddOAuthClientAuthentication();
83+
84+
WebApplication app = builder.Build();
85+
86+
// Configure the HTTP request pipeline.
87+
if (!app.Environment.IsDevelopment())
88+
{
89+
app.UseExceptionHandler("/Home/Error");
90+
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
91+
app.UseHsts();
92+
}
93+
94+
app.UseHttpsRedirection();
95+
app.UseStaticFiles();
96+
97+
app.UseRouting();
98+
99+
app.UseAuthorization();
100+
101+
app.MapControllerRoute(
102+
name: "default",
103+
pattern: "{controller=Home}/{action=Index}/{id?}");
104+
105+
app.Run();
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"iisSettings": {
3+
"windowsAuthentication": false,
4+
"anonymousAuthentication": true,
5+
"iisExpress": {
6+
"applicationUrl": "http://localhost:4797",
7+
"sslPort": 44305
8+
}
9+
},
10+
"profiles": {
11+
"WebApplication": {
12+
"commandName": "Project",
13+
"dotnetRunMessages": true,
14+
"launchBrowser": true,
15+
"applicationUrl": "https://localhost:7017;http://localhost:5017",
16+
"environmentVariables": {
17+
"ASPNETCORE_ENVIRONMENT": "Development"
18+
}
19+
},
20+
"IIS Express": {
21+
"commandName": "IISExpress",
22+
"launchBrowser": true,
23+
"environmentVariables": {
24+
"ASPNETCORE_ENVIRONMENT": "Development"
25+
}
26+
}
27+
}
28+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<h1>API Response</h1>
2+
3+
<pre>@ViewBag.Json</pre>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@{
2+
ViewData["Title"] = "Home Page";
3+
}
4+
5+
<div class="text-center">
6+
<h1 class="display-4">Welcome</h1>
7+
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
8+
</div>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@{
2+
ViewData["Title"] = "Privacy Policy";
3+
}
4+
<h1>@ViewData["Title"]</h1>
5+
6+
<p>Use this page to detail your site's privacy policy.</p>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
@model Web.Models.ErrorViewModel
2+
@{
3+
ViewData["Title"] = "Error";
4+
}
5+
6+
<h1 class="text-danger">Error.</h1>
7+
<h2 class="text-danger">An error occurred while processing your request.</h2>
8+
9+
@if (Model?.ShowRequestId ?? false)
10+
{
11+
<p>
12+
<strong>Request ID:</strong> <code>@Model?.RequestId</code>
13+
</p>
14+
}
15+
16+
<h3>Development Mode</h3>
17+
<p>
18+
Swapping to <strong>Development</strong> environment will display more detailed information about the error that occurred.
19+
</p>
20+
<p>
21+
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
22+
It can result in displaying sensitive information from exceptions to end users.
23+
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
24+
and restarting the app.
25+
</p>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8"/>
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
6+
<title>@ViewData["Title"] - WebApplication</title>
7+
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css"/>
8+
<link rel="stylesheet" href="~/css/site.css" asp-append-version="true"/>
9+
<link rel="stylesheet" href="~/WebApplication.styles.css" asp-append-version="true"/>
10+
</head>
11+
<body>
12+
<header>
13+
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
14+
<div class="container-fluid">
15+
<a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">WebApplication</a>
16+
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" aria-controls="navbarSupportedContent"
17+
aria-expanded="false" aria-label="Toggle navigation">
18+
<span class="navbar-toggler-icon"></span>
19+
</button>
20+
<div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
21+
<ul class="navbar-nav flex-grow-1">
22+
<li class="nav-item">
23+
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
24+
</li>
25+
<li class="nav-item">
26+
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
27+
</li>
28+
<li class="nav-item">
29+
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="CallApiAsClient">Call API</a>
30+
</li>
31+
</ul>
32+
</div>
33+
</div>
34+
</nav>
35+
</header>
36+
<div class="container">
37+
<main role="main" class="pb-3">
38+
@RenderBody()
39+
</main>
40+
</div>
41+
42+
<footer class="border-top footer text-muted">
43+
<div class="container">
44+
&copy; 2022 - WebApplication - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
45+
</div>
46+
</footer>
47+
<script src="~/lib/jquery/dist/jquery.min.js"></script>
48+
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
49+
<script src="~/js/site.js" asp-append-version="true"></script>
50+
@await RenderSectionAsync("Scripts", required: false)
51+
</body>
52+
</html>

0 commit comments

Comments
 (0)