Skip to content

Commit f262926

Browse files
committed
Update version to 0.4.0 & Use system.text.json instead of newtonsoft.
1 parent 722e15a commit f262926

12 files changed

+108
-51
lines changed

.github/workflows/dotnetcore.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
- name: Setup .NET Core
1313
uses: actions/setup-dotnet@v1
1414
with:
15-
dotnet-version: 3.1.100
15+
dotnet-version: 5.0.100
1616
- name: Build with dotnet
1717
run: dotnet build -c Release
1818
- name: Test with dotnet

demo/reCAPTCHA.Demo/Pages/V2_Checkbox.cshtml.cs

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
using System.Threading.Tasks;
1+
using System.Text.Json;
2+
using System.Threading.Tasks;
23
using Microsoft.AspNetCore.Mvc.RazorPages;
3-
using Newtonsoft.Json;
44
using Owl.reCAPTCHA;
55
using Owl.reCAPTCHA.v2;
66

@@ -22,10 +22,13 @@ public async Task OnPostAsync(string token)
2222
var response = await _siteVerify.Verify(new reCAPTCHASiteVerifyRequest
2323
{
2424
Response = token,
25-
RemoteIp = HttpContext.Connection.RemoteIpAddress.ToString()
25+
RemoteIp = HttpContext.Connection.RemoteIpAddress?.ToString()
2626
});
2727

28-
Result = JsonConvert.SerializeObject(response, Formatting.Indented);
28+
Result = JsonSerializer.Serialize(response, new JsonSerializerOptions
29+
{
30+
WriteIndented = true
31+
});
2932
}
3033
}
31-
}
34+
}

demo/reCAPTCHA.Demo/Pages/V2_Invisible.cshtml.cs

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
using System.Threading.Tasks;
1+
using System.Text.Json;
2+
using System.Threading.Tasks;
23
using Microsoft.AspNetCore.Mvc.RazorPages;
3-
using Newtonsoft.Json;
44
using Owl.reCAPTCHA;
55
using Owl.reCAPTCHA.v2;
66

@@ -22,10 +22,13 @@ public async Task OnPostAsync(string token)
2222
var response = await _siteVerify.Verify(new reCAPTCHASiteVerifyRequest
2323
{
2424
Response = token,
25-
RemoteIp = HttpContext.Connection.RemoteIpAddress.ToString()
25+
RemoteIp = HttpContext.Connection.RemoteIpAddress?.ToString()
2626
});
2727

28-
Result = JsonConvert.SerializeObject(response, Formatting.Indented);
28+
Result = JsonSerializer.Serialize(response, new JsonSerializerOptions
29+
{
30+
WriteIndented = true
31+
});
2932
}
3033
}
31-
}
34+
}

demo/reCAPTCHA.Demo/Pages/V3.cshtml.cs

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
using System.Threading.Tasks;
1+
using System.Text.Json;
2+
using System.Threading.Tasks;
23
using Microsoft.AspNetCore.Mvc.RazorPages;
3-
using Newtonsoft.Json;
44
using Owl.reCAPTCHA;
55
using Owl.reCAPTCHA.v3;
66

@@ -22,10 +22,13 @@ public async Task OnPostAsync(string token)
2222
var response = await _siteVerify.Verify(new reCAPTCHASiteVerifyRequest
2323
{
2424
Response = token,
25-
RemoteIp = HttpContext.Connection.RemoteIpAddress.ToString()
25+
RemoteIp = HttpContext.Connection.RemoteIpAddress?.ToString()
2626
});
2727

28-
Result = JsonConvert.SerializeObject(response, Formatting.Indented);
28+
Result = JsonSerializer.Serialize(response, new JsonSerializerOptions
29+
{
30+
WriteIndented = true
31+
});
2932
}
3033
}
31-
}
34+
}

demo/reCAPTCHA.Demo/Pages/V3_Programmatically.cshtml.cs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
using System.Threading.Tasks;
1+
using System.Text.Json;
2+
using System.Threading.Tasks;
23
using Microsoft.AspNetCore.Mvc.RazorPages;
3-
using Newtonsoft.Json;
44
using Owl.reCAPTCHA;
55
using Owl.reCAPTCHA.v3;
66

@@ -22,10 +22,13 @@ public async Task OnPostAsync(string token)
2222
var response = await _siteVerify.Verify(new reCAPTCHASiteVerifyRequest
2323
{
2424
Response = token,
25-
RemoteIp = HttpContext.Connection.RemoteIpAddress.ToString()
25+
RemoteIp = HttpContext.Connection.RemoteIpAddress?.ToString()
2626
});
2727

28-
Result = JsonConvert.SerializeObject(response, Formatting.Indented);
28+
Result = JsonSerializer.Serialize(response, new JsonSerializerOptions
29+
{
30+
WriteIndented = true
31+
});
2932
}
3033
}
3134
}

demo/reCAPTCHA.Demo/Startup.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public Startup(IConfiguration configuration)
1919
// This method gets called by the runtime. Use this method to add services to the container.
2020
public void ConfigureServices(IServiceCollection services)
2121
{
22-
services.AddRazorPages().AddRazorRuntimeCompilation();
22+
services.AddRazorPages();
2323

2424
services.AddreCAPTCHAV3(x =>
2525
{

demo/reCAPTCHA.Demo/reCAPTCHA.Demo.csproj

-10
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,6 @@
44
<TargetFramework>net5.0</TargetFramework>
55
</PropertyGroup>
66

7-
<ItemGroup>
8-
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.0" />
9-
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.0">
10-
<PrivateAssets>all</PrivateAssets>
11-
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
12-
</PackageReference>
13-
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="5.0.0" />
14-
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.0" />
15-
</ItemGroup>
16-
177
<ItemGroup>
188
<ProjectReference Include="..\..\src\Owl.reCAPTCHA\Owl.reCAPTCHA.csproj" />
199
</ItemGroup>

src/Owl.reCAPTCHA/Owl.reCAPTCHA.csproj

+4-8
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,20 @@
55
<RootNamespace>Owl.reCAPTCHA</RootNamespace>
66
<PackageId>Owl.reCAPTCHA</PackageId>
77
<Authors>maliming</Authors>
8-
<Description>reCAPTCHA for Asp Net Core 3.0</Description>
8+
<Description>reCAPTCHA for Asp Net Core 5.0</Description>
99
<PackageProjectUrl>https://github.com/maliming/reCAPTCHA</PackageProjectUrl>
1010
<RepositoryUrl>https://github.com/maliming/reCAPTCHA</RepositoryUrl>
1111
<RepositoryType>git</RepositoryType>
12-
<Version>0.3.0</Version>
12+
<Version>0.4.0</Version>
1313
<IsPackable>true</IsPackable>
1414
<OutputType>Library</OutputType>
1515
<PublishRepositoryUrl>true</PublishRepositoryUrl>
1616
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
17-
<!-- including PDB files in NuGet for source link because symbolsource.org does not support portable PDBs -->
18-
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
19-
<PackageVersion>0.3.1</PackageVersion>
17+
<PackageVersion>0.4.0</PackageVersion>
2018
</PropertyGroup>
2119

2220
<ItemGroup>
23-
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.0-beta-20204-02" PrivateAssets="All" />
24-
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
25-
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="5.0.0" />
21+
<FrameworkReference Include="Microsoft.AspNetCore.App" />
2622
</ItemGroup>
2723

2824
</Project>
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using Newtonsoft.Json;
32

43
namespace Owl.reCAPTCHA
54
{
@@ -9,13 +8,11 @@ public class reCAPTCHASiteVerifyResponse
98
public bool Success { get; set; }
109

1110
// timestamp of the challenge load (ISO format yyyy-MM-dd'T'HH:mm:ssZZ)
12-
[JsonProperty("challenge_ts")]
1311
public DateTime ChallengeTs { get; set; }
1412

1513
// the hostname of the site where the reCAPTCHA was solved
1614
public string HostName { get; set; }
1715

18-
[JsonProperty("error-codes")]
1916
public string[] ErrorCodes { get; set; }
2017
}
21-
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using System.Linq;
3+
using System.Text.Json;
4+
using System.Text.Json.Serialization;
5+
using Owl.reCAPTCHA.v3;
6+
7+
namespace Owl.reCAPTCHA
8+
{
9+
public class reCAPTCHASiteVerifyResponseJsonConverter : JsonConverter<reCAPTCHASiteVerifyResponse>
10+
{
11+
public override bool CanConvert(Type typeToConvert)
12+
{
13+
return typeToConvert == typeof(reCAPTCHASiteVerifyV3Response) ||
14+
typeToConvert == typeof(reCAPTCHASiteVerifyResponse);
15+
}
16+
17+
public override reCAPTCHASiteVerifyResponse Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
18+
{
19+
var rootElement = JsonDocument.ParseValue(ref reader).RootElement;
20+
21+
var success = rootElement.EnumerateObject().FirstOrDefault(x => x.Name.Equals("success", StringComparison.InvariantCultureIgnoreCase));
22+
var challengeTimestamp = rootElement.EnumerateObject().FirstOrDefault(x => x.Name.Equals("challenge_ts", StringComparison.InvariantCultureIgnoreCase));
23+
var hostname = rootElement.EnumerateObject().FirstOrDefault(x => x.Name.Equals("hostname", StringComparison.InvariantCultureIgnoreCase));
24+
var errorCodes = rootElement.EnumerateObject().FirstOrDefault(x => x.Name.Equals("error-codes", StringComparison.InvariantCultureIgnoreCase));
25+
26+
if (typeToConvert == typeof(reCAPTCHASiteVerifyResponse))
27+
{
28+
return new reCAPTCHASiteVerifyResponse
29+
{
30+
Success = success.Value.ValueKind == JsonValueKind.True,
31+
ChallengeTs = challengeTimestamp.Value.ValueKind == JsonValueKind.String ? challengeTimestamp.Value.GetDateTime() : DateTime.MinValue,
32+
HostName = hostname.Value.ValueKind == JsonValueKind.String ? hostname.Value.GetString() : null,
33+
ErrorCodes = errorCodes.Value.ValueKind == JsonValueKind.Array ? JsonSerializer.Deserialize<string[]>(errorCodes.Value.GetRawText()) : null
34+
};
35+
}
36+
37+
if (typeToConvert == typeof(reCAPTCHASiteVerifyV3Response))
38+
{
39+
var score = rootElement.EnumerateObject().FirstOrDefault(x => x.Name.Equals("score", StringComparison.InvariantCultureIgnoreCase));
40+
var action = rootElement.EnumerateObject().FirstOrDefault(x => x.Name.Equals("action", StringComparison.InvariantCultureIgnoreCase));
41+
return new reCAPTCHASiteVerifyV3Response
42+
{
43+
Success = success.Value.ValueKind == JsonValueKind.True,
44+
ChallengeTs = challengeTimestamp.Value.ValueKind == JsonValueKind.String ? challengeTimestamp.Value.GetDateTime() : DateTime.MinValue,
45+
HostName = hostname.Value.ValueKind == JsonValueKind.String ? hostname.Value.GetString() : null,
46+
ErrorCodes = errorCodes.Value.ValueKind == JsonValueKind.Array ? JsonSerializer.Deserialize<string[]>(errorCodes.Value.GetRawText()) : null,
47+
Score = score.Value.ValueKind == JsonValueKind.Number ? score.Value.GetSingle() : 0,
48+
Action = action.Value.ValueKind == JsonValueKind.String ? action.Value.GetString() : null
49+
};
50+
}
51+
52+
throw new JsonException($"Incorrect type: {typeToConvert.FullName}");
53+
}
54+
55+
public override void Write(Utf8JsonWriter writer, reCAPTCHASiteVerifyResponse value, JsonSerializerOptions options)
56+
{
57+
JsonSerializer.Serialize(writer, value);
58+
}
59+
}
60+
}

src/Owl.reCAPTCHA/v2/reCAPTCHASiteVerifyV2.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Net.Http;
4+
using System.Text.Json;
45
using System.Threading.Tasks;
56
using Microsoft.Extensions.Options;
6-
using Newtonsoft.Json;
77

88
namespace Owl.reCAPTCHA.v2
99
{
@@ -31,8 +31,9 @@ public async Task<reCAPTCHASiteVerifyResponse> Verify(reCAPTCHASiteVerifyRequest
3131
var v3Response = await _client.PostAsync("recaptcha/api/siteverify", content);
3232
if (v3Response.IsSuccessStatusCode)
3333
{
34-
return JsonConvert.DeserializeObject<reCAPTCHASiteVerifyResponse>(
35-
await v3Response.Content.ReadAsStringAsync());
34+
var options = new JsonSerializerOptions();
35+
options.Converters.Add(new reCAPTCHASiteVerifyResponseJsonConverter());
36+
return JsonSerializer.Deserialize<reCAPTCHASiteVerifyResponse>(await v3Response.Content.ReadAsStringAsync(), options);
3637
}
3738

3839
return new reCAPTCHASiteVerifyResponse
@@ -45,4 +46,4 @@ public async Task<reCAPTCHASiteVerifyResponse> Verify(reCAPTCHASiteVerifyRequest
4546
};
4647
}
4748
}
48-
}
49+
}

src/Owl.reCAPTCHA/v3/reCAPTCHASiteVerifyV3.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Net.Http;
4+
using System.Text.Json;
45
using System.Threading.Tasks;
56
using Microsoft.Extensions.Options;
6-
using Newtonsoft.Json;
77

88
namespace Owl.reCAPTCHA.v3
99
{
@@ -31,8 +31,9 @@ public async Task<reCAPTCHASiteVerifyV3Response> Verify(reCAPTCHASiteVerifyReque
3131
var v3Response = await _client.PostAsync("recaptcha/api/siteverify", content);
3232
if (v3Response.IsSuccessStatusCode)
3333
{
34-
return JsonConvert.DeserializeObject<reCAPTCHASiteVerifyV3Response>(
35-
await v3Response.Content.ReadAsStringAsync());
34+
var options = new JsonSerializerOptions();
35+
options.Converters.Add(new reCAPTCHASiteVerifyResponseJsonConverter());
36+
return JsonSerializer.Deserialize<reCAPTCHASiteVerifyV3Response>(await v3Response.Content.ReadAsStringAsync(), options);
3637
}
3738

3839
return new reCAPTCHASiteVerifyV3Response
@@ -45,4 +46,4 @@ public async Task<reCAPTCHASiteVerifyV3Response> Verify(reCAPTCHASiteVerifyReque
4546
};
4647
}
4748
}
48-
}
49+
}

0 commit comments

Comments
 (0)