Skip to content

Commit d28b105

Browse files
committed
Upgraded the DkimVerifier sample to net8.0
1 parent dc33173 commit d28b105

File tree

4 files changed

+55
-56
lines changed

4 files changed

+55
-56
lines changed

samples/DkimVerifier/DkimVerifier.sln

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
22
# Visual Studio Version 16
33
VisualStudioVersion = 16.0.29215.179
44
MinimumVisualStudioVersion = 10.0.40219.1
5-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DkimVerifier", "DkimVerifier\DkimVerifier.csproj", "{0871D5C3-BDF0-4FFF-9E2A-F94D49F4A382}"
5+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DkimVerifier", "DkimVerifier\DkimVerifier.csproj", "{0871D5C3-BDF0-4FFF-9E2A-F94D49F4A382}"
66
EndProject
77
Global
88
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,18 @@
1-
<?xml version="1.0" encoding="utf-8"?>
2-
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
1+
<Project Sdk="Microsoft.NET.Sdk">
32
<PropertyGroup>
4-
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5-
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6-
<ProjectGuid>{0871D5C3-BDF0-4FFF-9E2A-F94D49F4A382}</ProjectGuid>
3+
<TargetFramework>net8.0</TargetFramework>
74
<OutputType>Exe</OutputType>
8-
<RootNamespace>DkimVerifier</RootNamespace>
9-
<AssemblyName>DkimVerifier</AssemblyName>
10-
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
11-
<LangVersion>7.3</LangVersion>
12-
<TargetFrameworkProfile />
5+
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
136
</PropertyGroup>
147
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
15-
<PlatformTarget>AnyCPU</PlatformTarget>
16-
<DebugSymbols>true</DebugSymbols>
17-
<DebugType>full</DebugType>
18-
<OutputPath>bin\Debug\</OutputPath>
198
<DefineConstants>DEBUG;</DefineConstants>
20-
<ErrorReport>prompt</ErrorReport>
219
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
2210
</PropertyGroup>
2311
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU'">
24-
<PlatformTarget>AnyCPU</PlatformTarget>
25-
<OutputPath>bin\Release\</OutputPath>
26-
<Optimize>true</Optimize>
27-
<ErrorReport>prompt</ErrorReport>
2812
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
2913
</PropertyGroup>
3014
<ItemGroup>
31-
<Reference Include="System" />
32-
<Reference Include="System.Core" />
33-
<Reference Include="System.Data" />
34-
<Reference Include="System.Net.Http" />
35-
<Reference Include="System.Security" />
36-
</ItemGroup>
37-
<ItemGroup>
38-
<PackageReference Include="Heijden.Dns" version="2.0.0" />
15+
<PackageReference Include="DnsClient" Version="1.7.0" />
3916
<PackageReference Include="MimeKit" version="4.4.0" />
4017
</ItemGroup>
41-
<ItemGroup>
42-
<Compile Include="Program.cs" />
43-
<Compile Include="Properties\AssemblyInfo.cs" />
44-
</ItemGroup>
45-
<ItemGroup>
46-
<None Include="app.config" />
47-
</ItemGroup>
48-
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
4918
</Project>

samples/DkimVerifier/DkimVerifier/Program.cs

+48-18
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@
2626

2727
using System;
2828
using System.IO;
29+
using System.Net;
30+
using System.Linq;
2931
using System.Text;
3032
using System.Threading;
3133
using System.Threading.Tasks;
3234
using System.Collections.Generic;
3335

34-
using Heijden.DNS;
36+
using DnsClient;
3537

3638
using Org.BouncyCastle.Crypto;
3739

@@ -42,49 +44,57 @@ namespace DkimVerifierExample
4244
{
4345
class DkimPublicKeyLocator : DkimPublicKeyLocatorBase
4446
{
47+
static readonly char[] ColonDelimeter = new char[] { ':' };
4548
readonly Dictionary<string, AsymmetricKeyParameter> cache;
46-
readonly Resolver resolver;
49+
readonly LookupClient dnsClient;
4750

4851
public DkimPublicKeyLocator ()
4952
{
5053
cache = new Dictionary<string, AsymmetricKeyParameter> ();
5154

52-
resolver = new Resolver ("8.8.8.8") {
53-
TransportType = TransportType.Udp,
55+
var options = new LookupClientOptions (IPAddress.Parse ("8.8.8.8")) {
5456
UseCache = true,
5557
Retries = 3
5658
};
59+
60+
dnsClient = new LookupClient (options);
5761
}
5862

59-
AsymmetricKeyParameter DnsLookup (string domain, string selector, CancellationToken cancellationToken)
63+
AsymmetricKeyParameter GetPublicKey (string query, IDnsQueryResponse response)
6064
{
61-
var query = selector + "._domainkey." + domain;
62-
63-
// checked if we've already fetched this key
64-
if (cache.TryGetValue (query, out var pubkey))
65-
return pubkey;
66-
67-
// make a DNS query
68-
var response = resolver.Query (query, QType.TXT);
6965
var builder = new StringBuilder ();
7066

7167
// combine the TXT records into 1 string buffer
72-
foreach (var record in response.RecordsTXT) {
73-
foreach (var text in record.TXT)
68+
foreach (var record in response.Answers.TxtRecords ()) {
69+
foreach (var text in record.Text)
7470
builder.Append (text);
7571
}
7672

7773
var txt = builder.ToString ();
7874

79-
pubkey = GetPublicKey (txt);
75+
var pubkey = GetPublicKey (txt);
8076
cache.Add (query, pubkey);
8177

8278
return pubkey;
8379
}
8480

81+
AsymmetricKeyParameter DnsLookup (string domain, string selector, CancellationToken cancellationToken)
82+
{
83+
var query = selector + "._domainkey." + domain;
84+
85+
// checked if we've already fetched this key
86+
if (cache.TryGetValue (query, out var pubkey))
87+
return pubkey;
88+
89+
// make a DNS query
90+
var response = dnsClient.Query (query, QueryType.TXT, QueryClass.IN);
91+
92+
return GetPublicKey (query, response);
93+
}
94+
8595
public override AsymmetricKeyParameter LocatePublicKey (string methods, string domain, string selector, CancellationToken cancellationToken = default (CancellationToken))
8696
{
87-
var methodList = methods.Split (new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
97+
var methodList = methods.Split (ColonDelimeter, StringSplitOptions.RemoveEmptyEntries);
8898
for (int i = 0; i < methodList.Length; i++) {
8999
if (methodList[i] == "dns/txt")
90100
return DnsLookup (domain, selector, cancellationToken);
@@ -93,9 +103,29 @@ AsymmetricKeyParameter DnsLookup (string domain, string selector, CancellationTo
93103
throw new NotSupportedException (string.Format ("{0} does not include any suported lookup methods.", methods));
94104
}
95105

106+
async Task<AsymmetricKeyParameter> DnsLookupAsync (string domain, string selector, CancellationToken cancellationToken)
107+
{
108+
var query = selector + "._domainkey." + domain;
109+
110+
// checked if we've already fetched this key
111+
if (cache.TryGetValue (query, out var pubkey))
112+
return pubkey;
113+
114+
// make a DNS query
115+
var response = await dnsClient.QueryAsync (query, QueryType.TXT, QueryClass.IN, cancellationToken).ConfigureAwait (false);
116+
117+
return GetPublicKey (query, response);
118+
}
119+
96120
public override Task<AsymmetricKeyParameter> LocatePublicKeyAsync (string methods, string domain, string selector, CancellationToken cancellationToken = default (CancellationToken))
97121
{
98-
throw new NotImplementedException ("Asynchronous DKIM public key lookup is not implemented in this sample.");
122+
var methodList = methods.Split (ColonDelimeter, StringSplitOptions.RemoveEmptyEntries);
123+
for (int i = 0; i < methodList.Length; i++) {
124+
if (methodList[i] == "dns/txt")
125+
return DnsLookupAsync (domain, selector, cancellationToken);
126+
}
127+
128+
throw new NotSupportedException (string.Format ("{0} does not include any suported lookup methods.", methods));
99129
}
100130
}
101131

samples/DkimVerifier/DkimVerifier/Properties/AssemblyInfo.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
[assembly: AssemblyConfiguration ("")]
1010
[assembly: AssemblyCompany (".NET Foundation")]
1111
[assembly: AssemblyProduct ("")]
12-
[assembly: AssemblyCopyright ("Copyright © 2013-2023 .NET Foundation and Contributors")]
12+
[assembly: AssemblyCopyright ("Copyright © 2013-2024 .NET Foundation and Contributors")]
1313
[assembly: AssemblyTrademark (".NET Foundation")]
1414
[assembly: AssemblyCulture ("")]
1515

1616
// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
1717
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
1818
// and "{Major}.{Minor}.{Build}.*" will update just the revision.
1919

20-
[assembly: AssemblyVersion ("1.0.*")]
20+
[assembly: AssemblyVersion ("1.0.0")]
2121

2222
// The following attributes are used to specify the signing key for the assembly,
2323
// if desired. See the Mono documentation for more information about signing.

0 commit comments

Comments
 (0)