Skip to content

Commit 502bd23

Browse files
committed
(#272) [.Net 9] Add HybridCache
1 parent 8003fa5 commit 502bd23

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-1
lines changed

src/Monolith/ClassifiedAds.Infrastructure/Caching/CachingServiceCollectionExtensions.cs

+4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ public static IServiceCollection AddCaches(this IServiceCollection services, Cac
3838
});
3939
}
4040

41+
services.AddHybridCache(options =>
42+
{
43+
});
44+
4145
return services;
4246
}
4347
}

src/Monolith/ClassifiedAds.Infrastructure/ClassifiedAds.Infrastructure.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@
3333
<PackageReference Include="Microsoft.AspNetCore.SignalR.Protocols.MessagePack" Version="9.0.1" />
3434
<PackageReference Include="Microsoft.Azure.AppConfiguration.AspNetCore" Version="8.0.0" />
3535
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.1" />
36+
<PackageReference Include="Microsoft.Extensions.Caching.Hybrid" Version="9.4.0" />
3637
<PackageReference Include="Microsoft.Extensions.Caching.Redis" Version="2.3.0" />
3738
<PackageReference Include="Microsoft.Extensions.Caching.SqlServer" Version="9.0.1" />
38-
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.1" />
3939
<PackageReference Include="Microsoft.Extensions.Logging.AzureAppServices" Version="9.0.1" />
4040
<PackageReference Include="Microsoft.Extensions.Logging.EventLog" Version="9.0.1" />
4141
<PackageReference Include="Microsoft.Graph" Version="5.68.0" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using Microsoft.AspNetCore.Authentication;
2+
using Microsoft.Extensions.Caching.Hybrid;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Security.Claims;
7+
using System.Threading;
8+
using System.Threading.Tasks;
9+
10+
namespace ClassifiedAds.WebAPI.ClaimsTransformations;
11+
12+
public class CustomClaimsTransformation : IClaimsTransformation
13+
{
14+
private readonly HybridCache _cache;
15+
16+
public CustomClaimsTransformation(HybridCache cache)
17+
{
18+
_cache = cache;
19+
}
20+
21+
public async Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal)
22+
{
23+
var identity = principal.Identities.FirstOrDefault(x => x.IsAuthenticated);
24+
if (identity == null)
25+
{
26+
return principal;
27+
}
28+
29+
var userClaim = principal.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier);
30+
31+
if (Guid.TryParse(userClaim?.Value, out var userId))
32+
{
33+
var jti = principal.Claims.FirstOrDefault(x => x.Type == "jti").Value;
34+
var issuedAt = principal.Claims.FirstOrDefault(x => x.Type == "iat").Value;
35+
36+
var cacheKey = $"permissions/{userId}/{jti}/{issuedAt}";
37+
38+
var permissions = await _cache.GetOrCreateAsync(cacheKey,
39+
async (cancellationToken) => await GetPermissionsAsync(userId, cancellationToken),
40+
tags: ["permissions", $"permissions/{userId}"]);
41+
42+
var claims = new List<Claim>();
43+
claims.AddRange(permissions.Select(p => new Claim("Permission", p)));
44+
claims.AddRange(principal.Claims);
45+
46+
var newIdentity = new ClaimsIdentity(claims, identity.AuthenticationType);
47+
return new ClaimsPrincipal(newIdentity);
48+
}
49+
50+
return principal;
51+
}
52+
53+
private Task<List<string>> GetPermissionsAsync(Guid userId, CancellationToken cancellationToken)
54+
{
55+
// TODO: Get from Db
56+
var claims = new List<string>();
57+
return Task.FromResult(claims);
58+
}
59+
}

src/Monolith/ClassifiedAds.WebAPI/Program.cs

+4
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
using ClassifiedAds.Infrastructure.Web.ExceptionHandlers;
1616
using ClassifiedAds.Persistence;
1717
using ClassifiedAds.WebAPI.Authorization;
18+
using ClassifiedAds.WebAPI.ClaimsTransformations;
1819
using ClassifiedAds.WebAPI.ConfigurationOptions;
1920
using ClassifiedAds.WebAPI.Hubs;
2021
using ClassifiedAds.WebAPI.RateLimiterPolicies;
2122
using ClassifiedAds.WebAPI.Tenants;
23+
using Microsoft.AspNetCore.Authentication;
2224
using Microsoft.AspNetCore.Authentication.JwtBearer;
2325
using Microsoft.AspNetCore.Builder;
2426
using Microsoft.AspNetCore.DataProtection;
@@ -133,6 +135,8 @@
133135
};
134136
});
135137

138+
services.AddSingleton<IClaimsTransformation, CustomClaimsTransformation>();
139+
136140
services.AddAuthorizationPolicies(Assembly.GetExecutingAssembly(), AuthorizationPolicyNames.GetPolicyNames());
137141

138142
services.AddRateLimiter(options =>

0 commit comments

Comments
 (0)