Skip to content

Commit 2a21277

Browse files
authored
Merge pull request #7 from AppCoreNet/add-user-auth
Added OpenID Connect user authentication
2 parents b9c90e3 + 8925dfd commit 2a21277

File tree

41 files changed

+1286
-130
lines changed

Some content is hidden

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

41 files changed

+1286
-130
lines changed

AppCore.Extensions.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkerService", "Http\sampl
6464
EndProject
6565
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}"
6666
EndProject
67-
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}"
67+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AppCore.Extensions.Http.Authentication.OAuth.OpenIdConnect", "Http\src\AppCore.Extensions.Http.Authentication.OAuth.OpenIdConnect\AppCore.Extensions.Http.Authentication.OAuth.OpenIdConnect.csproj", "{91754F75-CC7A-48C8-A133-9F087BE10CFE}"
6868
EndProject
6969
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Web", "Http\samples\Web\Web.csproj", "{7D72ABA6-5EBE-491A-A599-65D15FD10308}"
7070
EndProject

Http/samples/Web/Controllers/HomeController.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,15 @@ public async Task<IActionResult> CallApiAsClient()
4646

4747
return View("CallApi");
4848
}
49+
50+
[Authorize]
51+
public async Task<IActionResult> CallApiAsUser()
52+
{
53+
HttpClient client = _httpClientFactory.CreateClient("api-user-client");
54+
55+
string response = await client.GetStringAsync("test");
56+
ViewBag.Json = JsonNode.Parse(response)!.ToString();
57+
58+
return View("CallApi");
59+
}
4960
}

Http/samples/Web/Program.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@
6969
o =>
7070
{
7171
o.Scope = "api";
72-
}));
72+
}))
73+
.AddOpenIdConnect();
7374

7475
// add HTTP client with OAuth client authentication
7576
builder.Services
@@ -81,6 +82,16 @@
8182
})
8283
.AddOAuthClientAuthentication();
8384

85+
// add HTTP client with OAuth user authentication
86+
builder.Services
87+
.AddHttpClient(
88+
"api-user-client",
89+
client =>
90+
{
91+
client.BaseAddress = new Uri("https://demo.duendesoftware.com/api/");
92+
})
93+
.AddOpenIdConnectAuthentication();
94+
8495
WebApplication app = builder.Build();
8596

8697
// Configure the HTTP request pipeline.
@@ -96,6 +107,7 @@
96107

97108
app.UseRouting();
98109

110+
app.UseAuthentication();
99111
app.UseAuthorization();
100112

101113
app.MapControllerRoute(

Http/samples/Web/Web.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<ProjectReference Include="..\..\src\AppCore.Extensions.Http.Authentication.OAuth.AspNetCore.OpenIdConnect\AppCore.Extensions.Http.Authentication.OAuth.AspNetCore.OpenIdConnect.csproj" />
9+
<ProjectReference Include="..\..\src\AppCore.Extensions.Http.Authentication.OAuth.OpenIdConnect\AppCore.Extensions.Http.Authentication.OAuth.OpenIdConnect.csproj" />
1010
</ItemGroup>
1111

1212
</Project>

Http/samples/Web/appsettings.Development.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"Logging": {
33
"LogLevel": {
4-
"Default": "Information",
4+
"Default": "Debug",
55
"Microsoft.AspNetCore": "Warning"
66
}
77
}

Http/src/AppCore.Extensions.Http.Authentication.OAuth.AspNetCore.OpenIdConnect/DependencyInjection/OAuthHttpClientAuthenticationBuilderExtensions.cs

Lines changed: 0 additions & 47 deletions
This file was deleted.

Http/src/AppCore.Extensions.Http.Authentication.OAuth.AspNetCore.OpenIdConnect/OpenIdConnectOAuthClientOptionsResolver.cs

Lines changed: 0 additions & 62 deletions
This file was deleted.

Http/src/AppCore.Extensions.Http.Authentication.OAuth.AspNetCore/AuthenticationSchemeOAuthClientOptionsResolver.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ namespace AppCore.Extensions.Http.Authentication.OAuth.AspNetCore;
1414
/// <summary>
1515
/// Provides the base class for resolving OAuth client authentication options from ASP.NET Core authentication schemes.
1616
/// </summary>
17-
/// <typeparam name="TClientOptions">The type of the <see cref="AuthenticationSchemeOAuthClientOptions"/>.</typeparam>
17+
/// <typeparam name="TClientOptions">The type of the <see cref="AuthenticationSchemeOptions"/>.</typeparam>
1818
/// <typeparam name="TOptions">The type of the <see cref="RemoteAuthenticationOptions"/>.</typeparam>
1919
/// <typeparam name="THandler">The type of the <see cref="IAuthenticationHandler"/>.</typeparam>
2020
public abstract class AuthenticationSchemeOAuthClientOptionsResolver<TClientOptions, TOptions, THandler> : IOAuthOptionsResolver
21-
where TClientOptions : AuthenticationSchemeOAuthClientOptions
21+
where TClientOptions : AuthenticationSchemeOptions
2222
where TOptions : RemoteAuthenticationOptions
2323
where THandler : IAuthenticationHandler
2424
{
@@ -56,12 +56,13 @@ protected AuthenticationSchemeOAuthClientOptionsResolver(
5656
&& typeof(T) == typeof(OAuthClientOptions))
5757
{
5858
TClientOptions clientOptions = _clientOptions.Get(scheme.Name);
59+
string? schemeName = GetSchemeName(clientOptions);
5960

6061
Microsoft.AspNetCore.Authentication.AuthenticationScheme? authenticationScheme =
61-
string.IsNullOrWhiteSpace(clientOptions.Scheme)
62+
string.IsNullOrWhiteSpace(schemeName)
6263
? await _authenticationSchemeProvider.GetDefaultChallengeSchemeAsync()
6364
.ConfigureAwait(false)
64-
: await _authenticationSchemeProvider.GetSchemeAsync(clientOptions.Scheme)
65+
: await _authenticationSchemeProvider.GetSchemeAsync(schemeName)
6566
.ConfigureAwait(false);
6667

6768
if (authenticationScheme is null)
@@ -86,6 +87,13 @@ protected AuthenticationSchemeOAuthClientOptionsResolver(
8687
return result;
8788
}
8889

90+
/// <summary>
91+
/// Gets the name of the authentication scheme.
92+
/// </summary>
93+
/// <param name="options"></param>
94+
/// <returns></returns>
95+
protected abstract string? GetSchemeName(TClientOptions options);
96+
8997
/// <summary>
9098
/// Must be implemented to resolve the <see cref="OAuthClientOptions"/> from the authentication scheme options.
9199
/// </summary>

0 commit comments

Comments
 (0)