Skip to content

Commit

Permalink
Upgrade to .NET 8.0, Close #166
Browse files Browse the repository at this point in the history
  • Loading branch information
VahidN committed Dec 11, 2023
1 parent 33278b2 commit 9c18c50
Show file tree
Hide file tree
Showing 15 changed files with 229 additions and 156 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ charset = utf-8-bom

[*.cs]

dotnet_diagnostic.ASP0023.severity = suggestion
dotnet_diagnostic.CA1510.severity = suggestion

dotnet_diagnostic.SYSLIB0021.severity = suggestion

dotnet_diagnostic.SYSLIB0020.severity = suggestion
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ jobs:
runs-on: windows-2019

steps:
- uses: actions/checkout@v2.3.4
- uses: actions/checkout@v4
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
uses: actions/setup-dotnet@v3
with:
dotnet-version: 7.0.400
dotnet-version: 8.0.x
- name: Build DNTCaptcha.Core lib
run: dotnet build ./src/DNTCaptcha.Core/DNTCaptcha.Core.csproj --configuration Release

Expand Down
86 changes: 86 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# ******** NOTE ********
# We have attempted to detect the languages in your repository. Please check
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: "CodeQL"

on:
push:
branches: [ "master" ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ "master" ]

jobs:
analyze:
name: Analyze
# Runner size impacts CodeQL analysis time. To learn more, please see:
# - https://gh.io/recommended-hardware-resources-for-running-codeql
# - https://gh.io/supported-runners-and-hardware-resources
# - https://gh.io/using-larger-runners
# Consider using larger runners for possible analysis time improvements.
runs-on: windows-2019
permissions:
actions: read
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: [ 'csharp' ]
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby', 'swift' ]
# Use only 'java' to analyze code written in Java, Kotlin or both
# Use only 'javascript' to analyze code written in JavaScript, TypeScript or both
# Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support

steps:
- name: Checkout repository
uses: actions/checkout@v4

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
config-file: ./.github/workflows/codeql/codeql-config.yml
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.

# For more details on CodeQL's query packs, refer to: https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
# queries: security-extended,security-and-quality


# Autobuild attempts to build any compiled languages (C/C++, C#, Go, Java, or Swift).
# If this step fails, then you should remove it and run the build manually (see below)
# - name: Autobuild
# uses: github/codeql-action/autobuild@v2
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 8.0.x
- name: Build
run: dotnet build --configuration Release

# ℹ️ Command-line programs to run using the OS shell.
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun

# If the Autobuild fails above, remove it and uncomment the following three lines.
# modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance.

# - run: |
# echo "Run, Build Application using script"
# ./location_of_script_within_repo/buildscript.sh

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
with:
category: "/language:${{matrix.language}}"
10 changes: 10 additions & 0 deletions .github/workflows/codeql/codeql-config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: "Security and Quality"

queries:
- uses: security-and-quality

query-filters:
- exclude:
id: cs/useless-if-statement
- exclude:
id: cs/empty-block
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "7.0.400",
"version": "8.0.100",
"rollForward": "latestMajor",
"allowPrerelease": false
}
Expand Down
58 changes: 22 additions & 36 deletions src/DNTCaptcha.Core/CaptchaServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ public static class CaptchaServiceCollectionExtensions
this IServiceCollection services,
Action<DNTCaptchaOptions>? options = null)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
if (services == null) throw new ArgumentNullException(nameof(services));

configOptions(services, options);

Expand All @@ -34,20 +31,20 @@ public static class CaptchaServiceCollectionExtensions
services.AddMvcCore().AddCookieTempDataProvider();


#if NET7_0
#if NET7_0 || NET8_0
// Also we need to have app.UseRateLimiter() after app.UseRouting()
services.AddRateLimiter(limiterOptions =>
limiterOptions.AddPolicy<string, DNTCaptchaRateLimiterPolicy>(
DNTCaptchaRateLimiterPolicy.Name));
limiterOptions.AddPolicy<string, DNTCaptchaRateLimiterPolicy>(
DNTCaptchaRateLimiterPolicy.Name));
#endif

services.TryAddSingleton<HumanReadableIntegerProvider>();
services.TryAddSingleton<ShowDigitsProvider>();
services.TryAddSingleton<SumOfTwoNumbersProvider>();
services.TryAddSingleton<SumOfTwoNumbersToWordsProvider>();
services.TryAddSingleton<Func<DisplayMode, ICaptchaTextProvider>>(serviceProvider =>
key => GetCaptchaTextProvider(key,
serviceProvider));
key => GetCaptchaTextProvider(key,
serviceProvider));

services.TryAddSingleton<IRandomNumberProvider, RandomNumberProvider>();
services.TryAddSingleton<ICaptchaImageProvider, CaptchaImageProvider>();
Expand All @@ -57,31 +54,28 @@ public static class CaptchaServiceCollectionExtensions
services.TryAddScoped<IDNTCaptchaApiProvider, DNTCaptchaApiProvider>();
services.TryAddSingleton<IActionContextAccessor, ActionContextAccessor>();
services.TryAddScoped<IUrlHelper>(serviceProvider =>
{
var actionContext = serviceProvider
.GetRequiredService<IActionContextAccessor>()
.ActionContext;
if (actionContext is null)
{
throw new InvalidOperationException("actionContext is null");
}
{
var actionContext = serviceProvider
.GetRequiredService<IActionContextAccessor>()
.ActionContext;
if (actionContext is null) throw new InvalidOperationException("actionContext is null");
var factory = serviceProvider.GetRequiredService<IUrlHelperFactory>();
return factory.GetUrlHelper(actionContext);
});
var factory = serviceProvider.GetRequiredService<IUrlHelperFactory>();
return factory.GetUrlHelper(actionContext);
});
}

private static ICaptchaTextProvider GetCaptchaTextProvider(DisplayMode key, IServiceProvider serviceProvider)
{
return key switch
{
DisplayMode.NumberToWord => serviceProvider.GetRequiredService<HumanReadableIntegerProvider>(),
DisplayMode.ShowDigits => serviceProvider.GetRequiredService<ShowDigitsProvider>(),
DisplayMode.SumOfTwoNumbers => serviceProvider.GetRequiredService<SumOfTwoNumbersProvider>(),
DisplayMode.SumOfTwoNumbersToWords =>
serviceProvider.GetRequiredService<SumOfTwoNumbersToWordsProvider>(),
_ => throw new InvalidOperationException($"Service of type {key} is not implemented."),
};
{
DisplayMode.NumberToWord => serviceProvider.GetRequiredService<HumanReadableIntegerProvider>(),
DisplayMode.ShowDigits => serviceProvider.GetRequiredService<ShowDigitsProvider>(),
DisplayMode.SumOfTwoNumbers => serviceProvider.GetRequiredService<SumOfTwoNumbersProvider>(),
DisplayMode.SumOfTwoNumbersToWords =>
serviceProvider.GetRequiredService<SumOfTwoNumbersToWordsProvider>(),
_ => throw new InvalidOperationException($"Service of type {key} is not implemented.")
};
}

private static void configOptions(IServiceCollection services, Action<DNTCaptchaOptions>? options)
Expand All @@ -96,24 +90,16 @@ private static void configOptions(IServiceCollection services, Action<DNTCaptcha
private static void setSerializationProvider(IServiceCollection services, DNTCaptchaOptions captchaOptions)
{
if (captchaOptions.CaptchaSerializationProvider == null)
{
services.TryAddSingleton<ISerializationProvider, InMemorySerializationProvider>();
}
else
{
services.TryAddSingleton(typeof(ISerializationProvider), captchaOptions.CaptchaSerializationProvider);
}
}

private static void setCaptchaStorageProvider(IServiceCollection services, DNTCaptchaOptions captchaOptions)
{
if (captchaOptions.CaptchaStorageProvider == null)
{
services.TryAddSingleton<ICaptchaStorageProvider, CookieCaptchaStorageProvider>();
}
else
{
services.TryAddSingleton(typeof(ICaptchaStorageProvider), captchaOptions.CaptchaStorageProvider);
}
}
}
17 changes: 10 additions & 7 deletions src/DNTCaptcha.Core/DNTCaptcha.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Description>DNTCaptcha.Core is a captcha generator and validator for ASP.NET Core applications.</Description>
<VersionPrefix>4.9.1</VersionPrefix>
<VersionPrefix>4.9.2</VersionPrefix>
<Authors>Vahid Nasiri</Authors>
<TargetFrameworks>net7.0;net6.0;</TargetFrameworks>
<TargetFrameworks>net8.0;net7.0;net6.0;</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AssemblyName>DNTCaptcha.Core</AssemblyName>
<PackageId>DNTCaptcha.Core</PackageId>
Expand Down Expand Up @@ -42,11 +42,11 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Meziantou.Analyzer" Version="2.0.62">
<PackageReference Include="Meziantou.Analyzer" Version="2.0.120">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.6.40">
<PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.8.14">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand All @@ -58,7 +58,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.4.0.72892">
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.15.0.81779">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand All @@ -67,17 +67,20 @@
<PlatformTarget>anycpu</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All"/>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All"/>
</ItemGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App"/>
<PackageReference Include="SkiaSharp" Version="2.88.6"/>
<PackageReference Include="SkiaSharp.HarfBuzz" Version="2.88.3"/>
<PackageReference Include="SkiaSharp.HarfBuzz" Version="2.88.6"/>
</ItemGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'net6.0'">
<DefineConstants>NET6_0</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'net7.0'">
<DefineConstants>NET7_0</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'net8.0'">
<DefineConstants>NET8_0</DefineConstants>
</PropertyGroup>
</Project>
Loading

0 comments on commit 9c18c50

Please sign in to comment.