Skip to content
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

Update to target netcoreapp 2.1 and netstandard 2.0. #34

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
15 changes: 7 additions & 8 deletions sample/TestWebApp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
using System.IO;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;

namespace TestWebApp
{
public class Program
public static class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
BuildWebHost(args).Run();
}

public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();

host.Run();
}
}
}
32 changes: 21 additions & 11 deletions sample/TestWebApp/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
#undef USE_NONCE

using Joonasw.AspNetCore.SecurityHeaders;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
Expand All @@ -9,18 +11,12 @@ namespace TestWebApp
{
public class Startup
{

public Startup(IHostingEnvironment env)
public Startup(IConfiguration configuration)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", true, true)
.AddUserSecrets();

Configuration = builder.Build();
Configuration = configuration;
}

public IConfigurationRoot Configuration { get; }
public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
Expand All @@ -34,6 +30,12 @@ public void ConfigureServices(IServiceCollection services)
SecretKey = Configuration["Recaptcha:SecretKey"],
ValidationMessage = "Are you a robot?"
});

#if (USE_NONCE)
{
services.AddCsp(nonceByteAmount: 32);
}
#endif
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand All @@ -44,6 +46,14 @@ public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)

app.UseDeveloperExceptionPage();

#if (USE_NONCE)
app.UseCsp(csp =>
{
csp.AllowScripts.AddNonce();
csp.SetReportOnly();
});
#endif

app.UseStaticFiles();

app.UseMvc(routes =>
Expand Down
63 changes: 27 additions & 36 deletions sample/TestWebApp/TestWebApp.csproj
Original file line number Diff line number Diff line change
@@ -1,50 +1,41 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp1.0</TargetFramework>
<TargetFramework>netcoreapp2.1</TargetFramework>
<PreserveCompilationContext>true</PreserveCompilationContext>
<AssemblyName>TestWebApp</AssemblyName>
<OutputType>Exe</OutputType>
<PackageId>TestWebApp</PackageId>
<UserSecretsId>TestWebApp-45EC5266-9512-4AF0-9BA1-F9C3DC7D57D0</UserSecretsId>
<RuntimeFrameworkVersion>1.0.4</RuntimeFrameworkVersion>
<PackageTargetFallback>$(PackageTargetFallback);dotnet5.6;dnxcore50;portable-net45+win8</PackageTargetFallback>
</PropertyGroup>

<AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback>
</PropertyGroup>

<ItemGroup>
<None Update="wwwroot\**\*;Views\**\*">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</None>
</ItemGroup>

<PropertyGroup>
<ServerGarbageCollection>true</ServerGarbageCollection>
</PropertyGroup>

</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\PaulMiami.AspNetCore.Mvc.Recaptcha\PaulMiami.AspNetCore.Mvc.Recaptcha.csproj" />
</ItemGroup>

<PackageReference Include="Joonasw.AspNetCore.SecurityHeaders" Version="2.7.0" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.1.5" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" Version="2.1.1" PrivateAssets="All" />
</ItemGroup>

<PropertyGroup>
<ServerGarbageCollection>true</ServerGarbageCollection>
<UserSecretsId>recaptcha-testweb-d2ab3bfe-f40f-4885-850b-9a303a9398ea</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="1.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.0.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="1.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.0.2" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="1.0.2" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.0.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.0.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.0.2" />
</ItemGroup>

<ProjectReference Include="..\..\src\PaulMiami.AspNetCore.Mvc.Recaptcha\PaulMiami.AspNetCore.Mvc.Recaptcha.csproj" />
</ItemGroup>

<Target Name="PrepublishScript" BeforeTargets="PrepareForPublish">
<Exec Command="bower install" />
</Target>

</Target>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="1.0.0" />
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.0" />
</ItemGroup>

</Project>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.4" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Description>This is a helper library for google reCAPTCHA 2.0
Expand All @@ -11,7 +11,7 @@ https://github.com/PaulMiami/reCAPTCHA/wiki/Change-log</Description>
<AssemblyTitle>reCAPTCHA 2.0 for ASPNET Core </AssemblyTitle>
<VersionPrefix>1.2.1</VersionPrefix>
<Authors>Paul Biccherai</Authors>
<TargetFrameworks>netstandard1.6;net451</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<AssemblyName>PaulMiami.AspNetCore.Mvc.Recaptcha</AssemblyName>
<PackageId>PaulMiami.AspNetCore.Mvc.Recaptcha</PackageId>
Expand All @@ -20,7 +20,7 @@ https://github.com/PaulMiami/reCAPTCHA/wiki/Change-log</Description>
<PackageLicenseUrl>https://raw.githubusercontent.com/PaulMiami/reCAPTCHA/master/LICENSE</PackageLicenseUrl>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/PaulMiami/reCAPTCHA</RepositoryUrl>
<NetStandardImplicitPackageVersion>1.6.0</NetStandardImplicitPackageVersion>
<NetStandardImplicitPackageVersion>2.0.0</NetStandardImplicitPackageVersion>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
Expand All @@ -32,15 +32,16 @@ https://github.com/PaulMiami/reCAPTCHA/wiki/Change-log</Description>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Localization" Version="1.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="1.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="1.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.ViewFeatures" Version="1.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Runtime" Version="1.0.2" />
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
<PackageReference Include="Joonasw.AspNetCore.SecurityHeaders" Version="2.7.0" />
<PackageReference Include="Microsoft.AspNetCore.Localization" Version="2.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.1.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.1.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.ViewFeatures" Version="2.1.3" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Runtime" Version="2.1.2" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net451' ">
<ItemGroup Condition=" '$(TargetFramework)' == 'net461' ">
<Reference Include="System" />
<Reference Include="System.Net.Http" />
<Reference Include="Microsoft.CSharp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
using Microsoft.AspNetCore.Razor.TagHelpers;
using System;
using Microsoft.AspNetCore.Localization;
using Joonasw.AspNetCore.SecurityHeaders.Csp;

namespace PaulMiami.AspNetCore.Mvc.Recaptcha.TagHelpers
{
public class RecaptchaScriptTagHelper : TagHelper
{
private IRecaptchaConfigurationService _service;
private IHttpContextAccessor _contextAccessor;
private readonly ICspNonceService _nonceService;
private readonly IRecaptchaConfigurationService _service;
private readonly IHttpContextAccessor _contextAccessor;

private const string _scriptSnippet = "var {0}=function(e){{var r=$('#{1}');r.length&&r.hide()}};$.validator.setDefaults({{submitHandler:function(){{var e=this,r=''!==grecaptcha.getResponse(),a='{2}',t=$('#{1}', e.currentForm);if(t.length===0)return !0;return a&&(r?t.length&&t.hide():(e.errorList.push({{message:a}}),$(e.currentForm).triggerHandler('invalid-form',[e]),t.length&&(t.html(a),t.show()))),r}}}});";

Expand All @@ -28,6 +30,15 @@ public RecaptchaScriptTagHelper(IRecaptchaConfigurationService service, IHttpCon

_service = service;
_contextAccessor = contextAccessor;
var services = contextAccessor.HttpContext.RequestServices;
if (!(services is null))
{
var nonceService = services.GetService(typeof(ICspNonceService));
if (!(nonceService is null))
{
_nonceService = nonceService as CspNonceService;
}
}
}

[HtmlAttributeName(JqueryValidationAttributeName)]
Expand Down Expand Up @@ -61,6 +72,12 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
if (JqueryValidation ?? true)
{
var script = new TagBuilder("script");

if (!(_nonceService is null))
{
script.Attributes.Add("nonce", _nonceService.GetNonce());
}

script.TagRenderMode = TagRenderMode.Normal;
script.InnerHtml.AppendHtml(string.Format(_scriptSnippet,
RecaptchaTagHelper.RecaptchaValidationJSCallBack, ValidationMessageElementId, _service.ValidationMessage));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace PaulMiami.AspNetCore.Mvc.Recaptcha.TagHelpers
{
public class RecaptchaTagHelper : TagHelper
{
private IRecaptchaConfigurationService _service;
private readonly IRecaptchaConfigurationService _service;

internal const string RecaptchaValidationJSCallBack = "recaptchaValidated";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,57 +1,54 @@
<Project Sdk="Microsoft.NET.Sdk">

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp1.0</TargetFramework>
<TargetFramework>netcoreapp2.1</TargetFramework>
<PreserveCompilationContext>true</PreserveCompilationContext>
<AssemblyName>PaulMiami.AspNetCore.Mvc.Recaptcha.Test</AssemblyName>
<PackageId>PaulMiami.AspNetCore.Mvc.Recaptcha.Test</PackageId>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<RuntimeFrameworkVersion>1.0.4</RuntimeFrameworkVersion>
<PackageTargetFallback>$(PackageTargetFallback);dotnet5.6;dnxcore50;portable-net45+win8</PackageTargetFallback>
<AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
</PropertyGroup>

</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\PaulMiami.AspNetCore.Mvc.Recaptcha\PaulMiami.AspNetCore.Mvc.Recaptcha.csproj" />
<ProjectReference Include="..\WebSites\TestSite\TestSite.csproj" />
</ItemGroup>

</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.ViewFeatures" Version="1.0.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="1.0.3" />
<PackageReference Include="Microsoft.Extensions.Logging.Testing" Version="1.2.0-preview1-23565" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.1.5" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="2.1.1" />
<PackageReference Include="Moq" Version="4.7.99" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp1.0' ">
</ItemGroup>
<!--<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp1.0' ">
<PackageReference Include="System.Diagnostics.TraceSource" Version="4.3.0" />
</ItemGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebugType>full</DebugType>
<DebugSymbols>True</DebugSymbols>
</PropertyGroup>
<ItemGroup>
<None Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

<!--
Found solution at https://github.com/aspnet/Razor/issues/1212
Work around https://github.com/NuGet/Home/issues/4412. MVC uses DependencyContext.Load() which looks next to a .dll
for a .deps.json. Information isn't available elsewhere. Need the .deps.json file for all web site applications.
-->
<Target Name="CopyDepsFiles" AfterTargets="Build" Condition="'$(TargetFramework)'!=''">
<ItemGroup>
<DepsFilePaths Include="$([System.IO.Path]::ChangeExtension('%(_ResolvedProjectReferencePaths.FullPath)', '.deps.json'))" />
</ItemGroup>

<Copy SourceFiles="%(DepsFilePaths.FullPath)" DestinationFolder="$(OutputPath)" Condition="Exists('%(DepsFilePaths.FullPath)')" />
</Target>

</Project>
</ItemGroup>-->
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebugType>full</DebugType>
<DebugSymbols>True</DebugSymbols>
</PropertyGroup>
<ItemGroup>
<None Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
<!--
Found solution at https://github.com/aspnet/Razor/issues/1212
Work around https://github.com/NuGet/Home/issues/4412. MVC uses DependencyContext.Load() which looks next to a .dll
for a .deps.json. Information isn't available elsewhere. Need the .deps.json file for all web site applications.
-->
<Target Name="CopyDepsFiles" AfterTargets="Build" Condition="'$(TargetFramework)'!=''">
<ItemGroup>
<DepsFilePaths Include="$([System.IO.Path]::ChangeExtension('%(_ResolvedProjectReferencePaths.FullPath)', '.deps.json'))" />
</ItemGroup>
<Copy SourceFiles="%(DepsFilePaths.FullPath)" DestinationFolder="$(OutputPath)" Condition="Exists('%(DepsFilePaths.FullPath)')" />
</Target>
</Project>
Loading