Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion AppCore.Extensions.sln
Original file line numberDiff line numberDiff line change
Expand Up@@ -64,7 +64,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkerService", "Http\sampl
EndProject
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}"
EndProject
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}"
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}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Web", "Http\samples\Web\Web.csproj", "{7D72ABA6-5EBE-491A-A599-65D15FD10308}"
EndProject
Expand Down
11 changes: 11 additions & 0 deletions Http/samples/Web/Controllers/HomeController.cs
Original file line numberDiff line numberDiff line change
Expand Up@@ -46,4 +46,15 @@ public async Task<IActionResult> CallApiAsClient()

return View("CallApi");
}

[Authorize]
public async Task<IActionResult> CallApiAsUser()
{
HttpClient client = _httpClientFactory.CreateClient("api-user-client");

string response = await client.GetStringAsync("test");
ViewBag.Json = JsonNode.Parse(response)!.ToString();

return View("CallApi");
}
}
14 changes: 13 additions & 1 deletion Http/samples/Web/Program.cs
Original file line numberDiff line numberDiff line change
Expand Up@@ -69,7 +69,8 @@
o =>
{
o.Scope = "api";
}));
}))
.AddOpenIdConnect();

// add HTTP client with OAuth client authentication
builder.Services
Expand All@@ -81,6 +82,16 @@
})
.AddOAuthClientAuthentication();

// add HTTP client with OAuth user authentication
builder.Services
.AddHttpClient(
"api-user-client",
client =>
{
client.BaseAddress = new Uri("https://demo.duendesoftware.com/api/");
})
.AddOpenIdConnectAuthentication();

WebApplication app = builder.Build();

// Configure the HTTP request pipeline.
Expand All@@ -96,6 +107,7 @@

app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.MapControllerRoute(
Expand Down
2 changes: 1 addition & 1 deletion Http/samples/Web/Web.csproj
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\AppCore.Extensions.Http.Authentication.OAuth.AspNetCore.OpenIdConnect\AppCore.Extensions.Http.Authentication.OAuth.AspNetCore.OpenIdConnect.csproj" />
<ProjectReference Include="..\..\src\AppCore.Extensions.Http.Authentication.OAuth.OpenIdConnect\AppCore.Extensions.Http.Authentication.OAuth.OpenIdConnect.csproj" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion Http/samples/Web/appsettings.Development.json
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Default": "Debug",
"Microsoft.AspNetCore": "Warning"
}
}
Expand Down

This file was deleted.

This file was deleted.

Original file line numberDiff line numberDiff line change
Expand Up@@ -14,11 +14,11 @@ namespace AppCore.Extensions.Http.Authentication.OAuth.AspNetCore;
/// <summary>
/// Provides the base class for resolving OAuth client authentication options from ASP.NET Core authentication schemes.
/// </summary>
/// <typeparam name="TClientOptions">The type of the <see cref="AuthenticationSchemeOAuthClientOptions"/>.</typeparam>
/// <typeparam name="TClientOptions">The type of the <see cref="AuthenticationSchemeOptions"/>.</typeparam>
/// <typeparam name="TOptions">The type of the <see cref="RemoteAuthenticationOptions"/>.</typeparam>
/// <typeparam name="THandler">The type of the <see cref="IAuthenticationHandler"/>.</typeparam>
public abstract class AuthenticationSchemeOAuthClientOptionsResolver<TClientOptions, TOptions, THandler> : IOAuthOptionsResolver
where TClientOptions : AuthenticationSchemeOAuthClientOptions
where TClientOptions : AuthenticationSchemeOptions
where TOptions : RemoteAuthenticationOptions
where THandler : IAuthenticationHandler
{
Expand DownExpand Up@@ -56,12 +56,13 @@ protected AuthenticationSchemeOAuthClientOptionsResolver(
&& typeof(T) == typeof(OAuthClientOptions))
{
TClientOptions clientOptions = _clientOptions.Get(scheme.Name);
string? schemeName = GetSchemeName(clientOptions);

Microsoft.AspNetCore.Authentication.AuthenticationScheme? authenticationScheme =
string.IsNullOrWhiteSpace(clientOptions.Scheme)
string.IsNullOrWhiteSpace(schemeName)
? await _authenticationSchemeProvider.GetDefaultChallengeSchemeAsync()
.ConfigureAwait(false)
: await _authenticationSchemeProvider.GetSchemeAsync(clientOptions.Scheme)
: await _authenticationSchemeProvider.GetSchemeAsync(schemeName)
.ConfigureAwait(false);

if (authenticationScheme is null)
Expand All@@ -86,6 +87,13 @@ protected AuthenticationSchemeOAuthClientOptionsResolver(
return result;
}

/// <summary>
/// Gets the name of the authentication scheme.
/// </summary>
/// <param name="options"></param>
/// <returns></returns>
protected abstract string? GetSchemeName(TClientOptions options);

/// <summary>
/// Must be implemented to resolve the <see cref="OAuthClientOptions"/> from the authentication scheme options.
/// </summary>
Expand Down
Loading