Skip to content

Commit cb47d43

Browse files
Move from sqids
1 parent 3cc0b4c commit cb47d43

File tree

4 files changed

+8
-19
lines changed

4 files changed

+8
-19
lines changed

Directory.Packages.props

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,10 @@
2424
<PackageVersion Include="IntelliTect.Multitool" Version="1.5.3" />
2525
<PackageVersion Include="Mailjet.Api" Version="3.0.0" />
2626
<PackageVersion Include="Microsoft.AspNetCore.Authentication.MicrosoftAccount" Version="8.0.12" />
27-
<PackageVersion Include="Microsoft.AspNetCore.Components" Version="9.0.1" />
2827
<PackageVersion Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.12" />
2928
<PackageVersion Include="Microsoft.AspNetCore.Identity.UI" Version="8.0.12" />
3029
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="8.0.12" />
3130
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.12" />
32-
<PackageVersion Include="Microsoft.AspNetCore.WebUtilities" Version="8.0.8" />
3331
<PackageVersion Include="Microsoft.CodeAnalysis.Common" Version="4.8.0" />
3432
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
3533
<PackageVersion Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.10" />
@@ -40,7 +38,6 @@
4038
<PackageVersion Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="9.0.0" />
4139
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
4240
<PackageVersion Include="Octokit" Version="14.0.0" />
43-
<PackageVersion Include="Sqids" Version="3.1.0" />
4441
<PackageVersion Include="xunit" Version="2.9.3" />
4542
<PackageVersion Include="xunit.runner.visualstudio" Version="3.0.1" />
4643
</ItemGroup>

EssentialCSharp.Web/EssentialCSharp.Web.csproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,9 @@
2222
<PackageReference Include="Mailjet.Api" />
2323
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" />
2424
<PackageReference Include="Microsoft.AspNetCore.Authentication.MicrosoftAccount" />
25-
<PackageReference Include="Microsoft.AspNetCore.Components" />
2625
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" />
2726
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" />
2827
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" />
29-
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" />
3028
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" />
3129
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools">
3230
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
@@ -35,7 +33,6 @@
3533
<PackageReference Include="Newtonsoft.Json" />
3634
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" />
3735
<PackageReference Include="Octokit" />
38-
<PackageReference Include="Sqids" />
3936
</ItemGroup>
4037
<ItemGroup>
4138
<Content Update="wwwroot\images\00mindmap.svg">

EssentialCSharp.Web/Program.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
using Microsoft.AspNetCore.HttpOverrides;
1010
using Microsoft.AspNetCore.Identity;
1111
using Microsoft.AspNetCore.Identity.UI.Services;
12-
using Microsoft.EntityFrameworkCore;
13-
using Sqids;
12+
using Microsoft.EntityFrameworkCore;
1413

1514
namespace EssentialCSharp.Web;
1615

@@ -107,13 +106,6 @@ private static void Main(string[] args)
107106
builder.Services.AddCaptchaService(builder.Configuration.GetSection(CaptchaOptions.CaptchaSender));
108107
builder.Services.AddSingleton<ISiteMappingService, SiteMappingService>();
109108
builder.Services.AddHostedService<DatabaseMigrationService>();
110-
builder.Services.AddSingleton(new SqidsEncoder<int>(new()
111-
{
112-
// This is a shuffled version of the default alphabet so the id's are at least unique to this site.
113-
// This being open source, it will be easy to decode the ids, but these id's are not meant to be secure.
114-
Alphabet = "imx4z2Ys7GZLXDqT5IkUOEnyvwPKJtp13NWdeuH6rRhCcQogjM8V09l",
115-
MinLength = 7,
116-
}));
117109
builder.Services.AddScoped<IReferralService, ReferralService>();
118110

119111
if (!builder.Environment.IsDevelopment())

EssentialCSharp.Web/Services/Referrals/ReferralService.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
using EssentialCSharp.Web.Extensions;
55
using Microsoft.AspNetCore.Identity;
66
using Microsoft.EntityFrameworkCore;
7-
using Sqids;
7+
using Microsoft.IdentityModel.Tokens;
88

99
namespace EssentialCSharp.Web.Services.Referrals;
1010

11-
public class ReferralService(EssentialCSharpWebContext dbContext, SqidsEncoder<int> sqids, UserManager<EssentialCSharpWebUser> userManager) : IReferralService
11+
public class ReferralService(EssentialCSharpWebContext dbContext, UserManager<EssentialCSharpWebUser> userManager) : IReferralService
1212
{
1313
public async Task<string?> GetReferralIdAsync(string userId)
1414
{
@@ -42,8 +42,11 @@ public class ReferralService(EssentialCSharpWebContext dbContext, SqidsEncoder<i
4242
}
4343
else
4444
{
45-
Random random = Random.Shared;
46-
referrerId = sqids.Encode(random.Next());
45+
do
46+
{
47+
referrerId = Base64UrlEncoder.Encode(Guid.NewGuid().ToByteArray())[..8];
48+
}
49+
while (dbContext.UserClaims.Any(claim => claim.ClaimType == ClaimsExtensions.ReferrerIdClaimType && claim.ClaimValue == referrerId));
4750

4851
await userManager.AddClaimAsync(user, new Claim(ClaimsExtensions.ReferrerIdClaimType, referrerId));
4952
return referrerId;

0 commit comments

Comments
 (0)