Skip to content

Update to net8 and support hide the badge. #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions .github/workflows/dotnetcore.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 7.0.100
- name: Build with dotnet
run: dotnet build -c Release
- name: Test with dotnet
run: dotnet test --no-restore
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup .NET Core
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x
- name: Build with dotnet
run: dotnet build -c Release
- name: Test with dotnet
run: dotnet test --no-restore
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Owl.reCAPTCHA
Google reCAPTCHA for ASP NET Core 7.0 (v3 and v2)
Google reCAPTCHA for ASP NET Core (v3 and v2)

# Install-Package

Expand Down Expand Up @@ -30,6 +30,12 @@ services.AddreCAPTCHAV3(x =>

<recaptcha-script-v3 />

@*
Hide-the-recaptcha-badge
https://developers.google.com/recaptcha/docs/faq#id-like-to-hide-the-recaptcha-badge.-what-is-allowed
<recaptcha-script-v3 hide-badge="true" />
*@

<script>
function callback(token) {
document.getElementById("token").value = token;
Expand Down Expand Up @@ -58,6 +64,13 @@ services.AddreCAPTCHAV3(x =>
</script>

<recaptcha-script-v3 />

@*
Hide-the-recaptcha-badge
https://developers.google.com/recaptcha/docs/faq#id-like-to-hide-the-recaptcha-badge.-what-is-allowed
<recaptcha-script-v3 hide-badge="true" />
*@

<recaptcha-script-v3-js action="login" execute="false" />
```

Expand Down Expand Up @@ -146,6 +159,12 @@ services.AddreCAPTCHAV2(x =>

<recaptcha-script-v2 onload="onload" />

@*
Hide-the-recaptcha-badge
https://developers.google.com/recaptcha/docs/faq#id-like-to-hide-the-recaptcha-badge.-what-is-allowed
<recaptcha-script-v2 hide-badge="true" />
*@

<form method="POST">
<input id="token" name="token" type="text" />
<input id="submit" type="submit" value="submit" />
Expand All @@ -168,6 +187,12 @@ services.AddreCAPTCHAV2(x =>

<recaptcha-script-v2 />

@*
Hide-the-recaptcha-badge
https://developers.google.com/recaptcha/docs/faq#id-like-to-hide-the-recaptcha-badge.-what-is-allowed
<recaptcha-script-v2 hide-badge="true" />
*@

<form id="demo-form" method="POST">
<input id="token" name="token" type="text" />
<button recaptcha-v2-callback="callback" recaptcha-v2-size="invisible">Submit</button>
Expand Down
3 changes: 1 addition & 2 deletions demo/reCAPTCHA.Demo/Pages/V2_Invisible.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
}
</script>

<recaptcha-script-v2 />
<recaptcha-script-v2 hide-badge="true" />

<form id="demo-form" method="POST">
<input id="token" name="token" type="text" />
<button recaptcha-v2-callback="callback" recaptcha-v2-size="invisible">Submit</button>
</form>

4 changes: 2 additions & 2 deletions demo/reCAPTCHA.Demo/Pages/V3.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@
}
</script>

<recaptcha-script-v3 />
<recaptcha-script-v3-js action="login" callback="callback" />
<recaptcha-script-v3 hide-badge="true" />
<recaptcha-script-v3-js action="login" callback="callback" />
2 changes: 1 addition & 1 deletion demo/reCAPTCHA.Demo/reCAPTCHA.Demo.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions src/Owl.reCAPTCHA/Owl.reCAPTCHA.csproj
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Owl.reCAPTCHA</RootNamespace>
<PackageId>Owl.reCAPTCHA</PackageId>
<Authors>maliming</Authors>
<Description>reCAPTCHA for ASP NET Core</Description>
<PackageProjectUrl>https://github.com/maliming/reCAPTCHA</PackageProjectUrl>
<RepositoryUrl>https://github.com/maliming/reCAPTCHA</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<Version>7.0.0</Version>
<Version>8.0.0</Version>
<IsPackable>true</IsPackable>
<OutputType>Library</OutputType>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageVersion>7.0.0</PackageVersion>
<PackageVersion>8.0.0</PackageVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
22 changes: 11 additions & 11 deletions src/Owl.reCAPTCHA/v2/TagHelpers/reCAPTCHAV2ScriptTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ public class reCAPTCHAV2ScriptTagHelper : TagHelper

public string Render { get; set; }

public bool HideBadge { get; set; }

private readonly reCAPTCHAOptions _options;

private readonly IreCAPTCHALanguageCodeProvider _reCAPTCHALanguageCodeProvider;

public reCAPTCHAV2ScriptTagHelper(IOptionsSnapshot<reCAPTCHAOptions> optionsAccessor,
public reCAPTCHAV2ScriptTagHelper(IOptionsSnapshot<reCAPTCHAOptions> optionsAccessor,
IreCAPTCHALanguageCodeProvider reCaptchaLanguageCodeProvider)
{
_options = optionsAccessor.Get(reCAPTCHAConsts.V2);
Expand All @@ -33,8 +35,7 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
*/

output.TagName = "script";
output.TagMode = TagMode.StartTagAndEndTag;
output.TagName = "";

var src = $"{_options.VerifyBaseUrl.RemovePostFix(StringComparison.OrdinalIgnoreCase, "/")}/recaptcha/api.js?" +
$"hl={_reCAPTCHALanguageCodeProvider.GetLanguageCode()}";
Expand All @@ -47,15 +48,14 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
src += $"&render={Render}";
}

output.Attributes.Add(new TagHelperAttribute("src", new HtmlString(src)));
var scriptAsync = ScriptAsync ? "async" : string.Empty;
var scriptDefer = ScriptDefer ? "defer" : string.Empty;

if (ScriptAsync)
{
output.Attributes.Add(new TagHelperAttribute("async"));
}
if (ScriptDefer)
output.Content.SetHtmlContent($"<script {scriptAsync} {scriptDefer} src=\"{src}\"></script>");

if (HideBadge)
{
output.Attributes.Add(new TagHelperAttribute("defer"));
output.PostElement.SetHtmlContent("<style>.grecaptcha-badge{visibility:hidden;}</style>");
}
}
}
}
13 changes: 9 additions & 4 deletions src/Owl.reCAPTCHA/v3/TagHelpers/reCAPTCHAV3ScriptTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace Owl.reCAPTCHA.v3.TagHelpers;
[HtmlTargetElement("recaptcha-script-v3", TagStructure = TagStructure.WithoutEndTag)]
public class reCAPTCHAV3ScriptTagHelper : TagHelper
{
public bool HideBadge { get; set; }

private readonly reCAPTCHAOptions _options;

private readonly IreCAPTCHALanguageCodeProvider _reCAPTCHALanguageCodeProvider;
Expand All @@ -24,12 +26,15 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
/*
<script src="https://www.google.com/recaptcha/api.js?render=_reCAPTCHA_site_key"></script>
*/

output.TagName = "script";
output.TagMode = TagMode.StartTagAndEndTag;
output.TagName = "";

var src = $"{_options.VerifyBaseUrl.RemovePostFix(StringComparison.OrdinalIgnoreCase, "/")}/recaptcha/api.js?hl={_reCAPTCHALanguageCodeProvider.GetLanguageCode()}&render={_options.SiteKey}";

output.Attributes.Add(new TagHelperAttribute("src", new HtmlString(src)));
output.Content.SetHtmlContent($"<script src=\"{src}\"></script>");

if (HideBadge)
{
output.PostElement.SetHtmlContent("<style>.grecaptcha-badge{visibility:hidden;}</style>");
}
}
}