Skip to content

Commit

Permalink
ADDED RedisAutoConfigurator class to configure Redis caching in the a…
Browse files Browse the repository at this point in the history
…pplication. The class includes methods to configure caching for the host application and the application.

DED AddWithConnectionString method to the Redis caching to the application using a connection string Redis caching to the application using a configuration section.
ADDED AddRedisCertificateLoader method to the connection string for Redis cache configuration.
MODIFIED AddRedisCaching method in the RedisWebApplicationBuilderExtensions class to use RedisEndpointCollectionProvider for Redis cache configuration.
MODIFIED AddRedisCertificateLoader method in the RedisWebApplicationBuilderExtensions class to use RedisCertificateLoaderAndValidator for Redis certificate loading and validation.
MODIFIED AddRedisCachingWithConnectionString method in the RedisWebApplicationBuilderExtensions class to use RedisEndpointCollectionProvider for Redis cache configuration.
  • Loading branch information
dgmjr committed Jan 30, 2024
1 parent d32e46d commit e9ed863
Show file tree
Hide file tree
Showing 21 changed files with 349 additions and 126 deletions.
15 changes: 13 additions & 2 deletions AzureAd/AppType.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
namespace Dgmjr.Identity.Web;
namespace Dgmjr.AzureAd.Web;

[Flags]
public enum AppType
{
Web = 1,

// Api = 2,
WebApi = 4,
RazorPages = 8,
Expand All @@ -21,7 +22,17 @@ public enum AppType

WebBased = Web | RazorPages | Mvc | WebApi | AzureFunction | AzureWebJob,

All = Web | RazorPages | Mvc | WebApi | AzureFunction | AzureWebJob | Console | Worker | Service | Function,
All =
Web
| RazorPages
| Mvc
| WebApi
| AzureFunction
| AzureWebJob
| Console
| Worker
| Service
| Function,

DesktopBased = Console | Worker | Service
}
2 changes: 1 addition & 1 deletion AzureAd/AutomaticAzureAdConfigurator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class AutomaticAzureAdConfigurator
{
public ConfigurationOrder Order => ConfigurationOrder.VeryEarly;

public void Configure(IHostApplicationBuilder builder)
public void Configure(WebApplicationBuilder builder)
{
builder.AddAzureAdB2CIdentity();
}
Expand Down
2 changes: 1 addition & 1 deletion AzureAd/AzureAdApplicationBuilderIdentityExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Microsoft.Extensions.DependencyInjection;

using Dgmjr.Identity.Web;
using Dgmjr.AzureAd.Web;

public static class AzureAdApplicationBuilderIdentityExtensions
{
Expand Down
54 changes: 35 additions & 19 deletions AzureAd/AzureAdHostApplicationBuilderIdentityExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@ namespace Microsoft.Extensions.DependencyInjection;

using System.Net.Http;

using Dgmjr.Identity.Web;
using Dgmjr.AzureAd.Web;

using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Server;
using Microsoft.Extensions.Logging;
using Microsoft.Identity.Web.Resource;
using Microsoft.Identity.Web.UI;
using MicrosoftIdentityOptions = Dgmjr.Identity.Web.MicrosoftIdentityOptions;
using MicrosoftIdentityOptions = Dgmjr.AzureAd.Web.MicrosoftIdentityOptions;

public static class AzureAdHostApplicationBuilderIdentityExtensions
{
public static IHostApplicationBuilder AddAzureAdB2CIdentity(
this IHostApplicationBuilder builder
)
public static WebApplicationBuilder AddAzureAdB2CIdentity(this WebApplicationBuilder builder)
{
if (
builder.Services.Any(
Expand Down Expand Up @@ -73,6 +71,8 @@ this IHostApplicationBuilder builder
)
.AddDistributedTokenCaches();

callsWebApiAuthenticationBuilder.AddSessionTokenCaches();

foreach (
var downstreamApiConfig in builder.Configuration
.GetSection(DownstreamApis)
Expand All @@ -81,17 +81,10 @@ var downstreamApiConfig in builder.Configuration
{
callsWebApiAuthenticationBuilder.AddDownstreamApi(
downstreamApiConfig.Key,
downstreamApiConfig
);
}

builder.Services
.AddMicrosoftIdentityConsentHandler()
.AddTransient<Microsoft.Identity.Web.UI.Areas.MicrosoftIdentity.Controllers.AccountController>();
builder.Services
.ConfigureAll<DownstreamApiOptions>(
downstreamApiOptions =>
downstreamApiOptions.Serializer = requestObject =>
options =>
{
downstreamApiConfig.Bind(options);
options.Serializer = requestObject =>
new StringContent(
Serialize(
requestObject,
Expand All @@ -103,12 +96,35 @@ var downstreamApiConfig in builder.Configuration
>()
.CurrentValue.JsonSerializerOptions
)
)
)
);
}
);
}

builder.Services
.AddMicrosoftIdentityConsentHandler()
.AddTransient<Microsoft.Identity.Web.UI.Areas.MicrosoftIdentity.Controllers.AccountController>();
builder.Services
// .ConfigureAll<DownstreamApiOptions>(
// downstreamApiOptions =>
// downstreamApiOptions.Serializer = requestObject =>
// new StringContent(
// Serialize(
// requestObject,
// builder.Services
// .BuildServiceProvider()
// .CreateScope()
// .ServiceProvider.GetRequiredService<
// IOptionsMonitor<JsonOptions>
// >()
// .CurrentValue.JsonSerializerOptions
// )
// )
// )
.Configure<MicrosoftIdentityApplicationOptions>(
builder.Configuration.GetSection(AzureAdB2C)
)
.Configure<Dgmjr.Identity.Web.MicrosoftIdentityOptions>(
.Configure<Dgmjr.AzureAd.Web.MicrosoftIdentityOptions>(
builder.Configuration.GetSection(AzureAdB2C)
)
.Configure<MicrosoftGraphOptions>(
Expand Down
14 changes: 4 additions & 10 deletions AzureAd/LoggerExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using Dgmjr.Identity.Web;
namespace Dgmjr.AzureAd;

using Microsoft.Extensions.Logging;

namespace Dgmjr.AzureAd;
using Dgmjr.AzureAd.Web;

public static partial class LoggerExtensions
{
Expand All @@ -12,18 +11,13 @@ public static partial class LoggerExtensions
Message = "Registering Microsoft Identity Web UI.",
EventName = nameof(RegisteringIdentityWebUi)
)]
public static partial void RegisteringIdentityWebUi(this ILogger logger);

public static partial void RegisteringIdentityWebUi(
this ILogger logger
);
[LoggerMessage(
EventId = 2,
Level = LogLevel.Trace,
Message = "Registering app of type {AppType}",
EventName = nameof(RegisteringAppWithType)
)]
public static partial void RegisteringAppWithType(
this ILogger logger,
AppType appType
);
public static partial void RegisteringAppWithType(this ILogger logger, AppType appType);
}
3 changes: 2 additions & 1 deletion AzureAd/MicrosoftIdentityOptions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
namespace Dgmjr.Identity.Web;
namespace Dgmjr.AzureAd.Web;

public class MicrosoftIdentityOptions : Microsoft.Identity.Web.MicrosoftIdentityOptions
{
public AppType AppType { get; set; } = AppType.WebApi;
public string DefaultFallbackRoute { get; set; } = "/index";
public string[] InitialScopes { get; set; } = Empty<string>();
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<ItemGroup>
<Using Include="Microsoft.Extensions.Caching.Distributed" />
<Using Include="Dgmjr.Extensions.Caching" />
<Using Include="Dgmjr.Caching.Extensions" />
<Using Include="Microsoft.Extensions.Logging" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Microsoft Visual Studio Solution File, Format Version 12.00
#
#
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{B283EBC2-E01F-412D-9339-FD56EF114549}"
ProjectSection(SolutionItems) = preProject
..\Directory.Build.props = ..\Directory.Build.props
Expand All @@ -8,7 +8,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
..\..\..\..\Packages\Versions.Local.props = ..\..\..\..\Packages\Versions.Local.props
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dgmjr.Extensions.Caching", "Dgmjr.Extensions.Caching.csproj", "{E00F9E2C-7B1F-4083-9F4B-78D0CEE6BF0F}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dgmjr.Caching.Extensions", "Dgmjr.Caching.Extensions.csproj", "{E00F9E2C-7B1F-4083-9F4B-78D0CEE6BF0F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
96 changes: 81 additions & 15 deletions Caching/DistributedCacheExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,37 +163,103 @@ public static class DistributedCacheExtensions
return result;
}

public static Task SetAsync(this IDistributedCache cache, object key, object value, DateTimeOffset? absoluteExpiration = default, TimeSpan? absoluteExpirationRelativeToNow = default, TimeSpan? slidingExpiration = default, CancellationToken cancellationToken = default)
public static Task SetAsync(
this IDistributedCache cache,
object key,
object value,
DateTimeOffset? absoluteExpiration = default,
TimeSpan? absoluteExpirationRelativeToNow = default,
TimeSpan? slidingExpiration = default,
CancellationToken cancellationToken = default
)
{
return cache.SetAsync(Serialize(key, Jso), value, absoluteExpiration, absoluteExpirationRelativeToNow, slidingExpiration, cancellationToken);
return cache.SetAsync(
Serialize(key, Jso),
value,
absoluteExpiration,
absoluteExpirationRelativeToNow,
slidingExpiration,
cancellationToken
);
}

public static async Task SetAsync(this IDistributedCache cache, string key, object value, DateTimeOffset? absoluteExpiration = default, TimeSpan? absoluteExpirationRelativeToNow = default, TimeSpan? slidingExpiration = default, CancellationToken cancellationToken = default)
public static async Task SetAsync(
this IDistributedCache cache,
string key,
object value,
DateTimeOffset? absoluteExpiration = default,
TimeSpan? absoluteExpirationRelativeToNow = default,
TimeSpan? slidingExpiration = default,
CancellationToken cancellationToken = default
)
{
await cache.SetStringAsync(key, Serialize(value, Jso), absoluteExpiration, absoluteExpirationRelativeToNow, slidingExpiration, cancellationToken);
await cache.SetStringAsync(
key,
Serialize(value, Jso),
absoluteExpiration,
absoluteExpirationRelativeToNow,
slidingExpiration,
cancellationToken
);
}

public static Task SetStringAsync(this IDistributedCache cache, object key, string value, DateTimeOffset? absoluteExpiration = default, TimeSpan? absoluteExpirationRelativeToNow = default, TimeSpan? slidingExpiration = default, CancellationToken cancellationToken = default)
public static Task SetStringAsync(
this IDistributedCache cache,
object key,
string value,
DateTimeOffset? absoluteExpiration = default,
TimeSpan? absoluteExpirationRelativeToNow = default,
TimeSpan? slidingExpiration = default,
CancellationToken cancellationToken = default
)
{
return cache.SetStringAsync(Serialize(key, Jso), value, absoluteExpiration, absoluteExpirationRelativeToNow, slidingExpiration, cancellationToken);
return cache.SetStringAsync(
Serialize(key, Jso),
value,
absoluteExpiration,
absoluteExpirationRelativeToNow,
slidingExpiration,
cancellationToken
);
}

public static Task SetStringAsync(this IDistributedCache cache, string key, string value, DateTimeOffset? absoluteExpiration = default, TimeSpan? absoluteExpirationRelativeToNow = default, TimeSpan? slidingExpiration = default, CancellationToken cancellationToken = default)
public static Task SetStringAsync(
this IDistributedCache cache,
string key,
string value,
DateTimeOffset? absoluteExpiration = default,
TimeSpan? absoluteExpirationRelativeToNow = default,
TimeSpan? slidingExpiration = default,
CancellationToken cancellationToken = default
)
{
return cache.SetStringAsync(key, value, new DistributedCacheEntryOptions
{
AbsoluteExpiration = absoluteExpiration,
AbsoluteExpirationRelativeToNow = absoluteExpirationRelativeToNow,
SlidingExpiration = slidingExpiration
}, cancellationToken);
return cache.SetStringAsync(
key,
value,
new DistributedCacheEntryOptions
{
AbsoluteExpiration = absoluteExpiration,
AbsoluteExpirationRelativeToNow = absoluteExpirationRelativeToNow,
SlidingExpiration = slidingExpiration
},
cancellationToken
);
}

public static async Task<TValue> GetAsync<TValue>(this IDistributedCache cache, object key, CancellationToken cancellationToken = default)
public static async Task<TValue?> GetAsync<TValue>(
this IDistributedCache cache,
object key,
CancellationToken cancellationToken = default
)
{
return await cache.GetAsync<TValue>(Serialize(key, Jso), cancellationToken);
}

public static async Task<TValue> GetAsync<TValue>(this IDistributedCache cache, string key, CancellationToken cancellationToken = default)
public static async Task<TValue?> GetAsync<TValue>(
this IDistributedCache cache,
string key,
CancellationToken cancellationToken = default
)
{
return Deserialize<TValue>(await cache.GetAsync(key, cancellationToken), Jso);
}
Expand Down
3 changes: 2 additions & 1 deletion Caching/LoggerExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Dgmjr.Extensions.Caching;
namespace Dgmjr.Caching.Extensions;

using static Microsoft.Extensions.Logging.LogLevel;

public static partial class LoggerExtensions
Expand Down
4 changes: 2 additions & 2 deletions Caching/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ title: DGMJR Caching Extensions
lastmod: 2023-56-27T13:56:34.5772-05:00Z
date: 2023-56-27T13:56:34.5773-05:00Z
license: MIT
slug: Dgmjr.Extensions.Caching-readme
slug: Dgmjr.Caching.Extensions-readme
version:
authors:
- dgmjr
description: Contains extension methods and classes for working with caches
keywords:
- Dgmjr.Extensions.Caching
- Dgmjr.Caching.Extensions
- dgmjr
- dgmjr-io
type: readme
Expand Down
11 changes: 9 additions & 2 deletions Logging/AutomaticLoggingConfigurator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,23 @@ namespace Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Hosting;

using Serilog;

public class AutomaticLoggingConfigurator
: IConfigureIHostApplicationBuilder,
IConfigureIApplicationBuilder
{
public ConfigurationOrder Order => ConfigurationOrder.VeryEarly;

public void Configure(IHostApplicationBuilder builder)
public void Configure(WebApplicationBuilder builder)
{
builder.AddLoggingAndApplicationInsightsTelemetry();
// builder.Services.Add
}

public void Configure(IApplicationBuilder app) { }
public void Configure(IApplicationBuilder app)
{
app.UseHttpLogging();
app.UseSerilogRequestLogging();
}
}
9 changes: 9 additions & 0 deletions Logging/Dgmjr.Logging.Extensions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" />
<PackageReference Include="OpenTelemetry.Instrumentation.SqlClient" />

<PackageReference Include="Serilog.Extensions.Logging" />
<PackageReference Include="Serilog.Sinks.Console" />
<PackageReference Include="Serilog.Sinks.File" />
<PackageReference Include="Serilog.Sinks.MsSqlServer" />
<PackageReference Include="Serilog.Sinks.OpenTelemetry" />
<PackageReference Include="Serilog.Sinks.ApplicationInsights" />
<PackageReference Include="Serilog.AspNetCore" />
<PackageReference Include="Serilog.Sinks.AzureApp " />
</ItemGroup>
<ItemGroup>
<Content Include="LICENSE.md" Pack="true" PackagePath="LICENSE.md" />
Expand Down
Loading

0 comments on commit e9ed863

Please sign in to comment.