File tree Expand file tree Collapse file tree 4 files changed +56
-4
lines changed Expand file tree Collapse file tree 4 files changed +56
-4
lines changed Original file line number Diff line number Diff line change 1616 <PackageVersion Include =" ContentFeedNuget" Version =" $(ToolingPackagesVersion)" />
1717 </ItemGroup >
1818 <ItemGroup >
19- <PackageVersion Include =" Microsoft.AspNetCore.Components" Version =" 9.0.1" />
19+ <PackageVersion Include =" Sqids" Version =" 3.1.0" />
20+ <PackageVersion Include =" Microsoft.EntityFrameworkCore.Sqlite" Version =" 8.0.8" />
2021 <PackageVersion Include =" Microsoft.CodeAnalysis.CSharp" Version =" 4.8.0" />
2122 <PackageVersion Include =" Microsoft.CodeAnalysis.Common" Version =" 4.8.0" />
2223 <PackageVersion Include =" EssentialCSharp.Shared.Models" Version =" $(ToolingPackagesVersion)" />
Original file line number Diff line number Diff line change 3333 <PackageReference Include =" Newtonsoft.Json" />
3434 <PackageReference Include =" Microsoft.VisualStudio.Azure.Containers.Tools.Targets" />
3535 <PackageReference Include =" Octokit" />
36+ <PackageReference Include =" Sqids" />
3637 </ItemGroup >
3738 <ItemGroup >
3839 <Content Update =" wwwroot\images\00mindmap.svg" >
Original file line number Diff line number Diff line change 88using Microsoft . AspNetCore . HttpOverrides ;
99using Microsoft . AspNetCore . Identity ;
1010using Microsoft . AspNetCore . Identity . UI . Services ;
11- using Microsoft . EntityFrameworkCore ;
11+ using Microsoft . EntityFrameworkCore ;
12+ using Sqids ;
1213
1314namespace EssentialCSharp . Web ;
1415
@@ -104,8 +105,14 @@ private static void Main(string[] args)
104105 builder . Services . AddRazorPages ( ) ;
105106 builder . Services . AddCaptchaService ( builder . Configuration . GetSection ( CaptchaOptions . CaptchaSender ) ) ;
106107 builder . Services . AddSingleton < ISiteMappingService , SiteMappingService > ( ) ;
107- builder . Services . AddHostedService < DatabaseMigrationService > ( ) ;
108-
108+ builder . Services . AddHostedService < DatabaseMigrationService > ( ) ;
109+ builder . Services . AddSingleton ( new SqidsEncoder < int > ( new ( )
110+ {
111+ // This is a shuffled version of the default alphabet so the id's are at least unique to this site.
112+ // This being open source, it will be easy to decode the ids, but these id's are not meant to be secure.
113+ Alphabet = "imx4BSz2Ys7GZLXDqT5IAkUOEnyvwbPKJtp13NWdeuH6rFfRhCcQogjaM8V09l" ,
114+ MinLength = 10 ,
115+ } ) ) ;
109116
110117 if ( ! builder . Environment . IsDevelopment ( ) )
111118 {
Original file line number Diff line number Diff line change 1+ using Sqids ;
2+
3+ namespace EssentialCSharp . Web . Services ;
4+
5+ public class ReferrerService ( SqidsEncoder < int > sqids )
6+ {
7+ public string GenerateReferrerLink ( string baseUrl , string userId )
8+ {
9+ string referrerId = sqids . Encode ( 1 , 2 , 3 ) ;
10+ string referrerLink = $ "{ baseUrl } ?referrerId={ referrerId } ";
11+
12+ // Store the referrerId and userId in the database for tracking
13+ SaveReferrerIdToDatabase ( userId , referrerId ) ;
14+
15+ return referrerLink ;
16+ }
17+
18+ private void SaveReferrerIdToDatabase ( string userId , string referrerId )
19+ {
20+ // Implement your database logic here
21+ }
22+
23+ /// <summary>
24+ /// Track the referral in the database.
25+ /// </summary>
26+ /// <param name="referrerId">The referrer ID to track.</param>
27+ /// <returns>True if the referral was successfully tracked, otherwise false.</returns>
28+ public bool TrackReferral ( string referrerId )
29+ {
30+ // Implement your logic to track the referral in the database
31+
32+ if ( sqids . Decode ( referrerId ) is [ var decodedId ] &&
33+ referrerId == sqids . Encode ( decodedId ) )
34+ {
35+ // `incomingId` decodes into a single number and is canonical, here you can safely proceed with the rest of the logic
36+ }
37+ else
38+ {
39+ // consider `incomingId` invalid — e.g. respond with 404
40+ }
41+ IReadOnlyList < int > numbers = sqids . Decode ( referrerId ) ; // [1, 2, 3]
42+ }
43+ }
You can’t perform that action at this time.
0 commit comments