--no-https option for RazorPagesWeb-CSharp template (#362)

This commit is contained in:
Jass Bagga 2018-03-29 13:49:20 -07:00 committed by GitHub
parent 5ae68a012c
commit aca30fe8a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 66 additions and 13 deletions

View File

@ -29,7 +29,7 @@
<PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="${MicrosoftAspNetCoreAuthenticationCookiesPackageVersion}" Condition="'$(IndividualLocalAuth)' == 'True'" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="${MicrosoftAspNetCoreDiagnosticsEntityFrameworkCorePackageVersion}" Condition="'$(IndividualLocalAuth)' == 'True'" />
<PackageReference Include="Microsoft.AspNetCore.CookiePolicy" Version="${MicrosoftAspNetCoreCookiePolicyPackageVersion}" />
<PackageReference Include="Microsoft.AspNetCore.HttpsPolicy" Version="${MicrosoftAspNetCoreHttpsPolicyPackageVersion}" />
<PackageReference Include="Microsoft.AspNetCore.HttpsPolicy" Version="${MicrosoftAspNetCoreHttpsPolicyPackageVersion}" Condition="'$(RequiresHttps)' == 'True'"/>
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="${MicrosoftAspNetCoreIdentityEntityFrameworkCorePackageVersion}" Condition="'$(IndividualLocalAuth)' == 'True'" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="${MicrosoftAspNetCoreIdentityEntityFrameworkCorePackageVersion}" Condition="'$(IndividualLocalAuth)' == 'True'" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="${MicrosoftAspNetCoreMvcPackageVersion}" />

View File

@ -72,6 +72,10 @@
"UseBrowserLink": {
"longName": "use-browserlink",
"shortName": ""
},
"NoHttps": {
"longName": "no-https",
"shortName": ""
}
},
"usageExamples": [

View File

@ -246,7 +246,7 @@
"HttpsPort": {
"type": "parameter",
"datatype": "integer",
"description": "Port number to use for the HTTPS endpoint in launchSettings.json."
"description": "Port number to use for the HTTPS endpoint in launchSettings.json. This option is only applicable when the parameter no-https is not used (no-https will be ignored if either IndividualAuth or OrganizationalAuth is used)."
},
"HttpsPortGenerated": {
"type": "generated",
@ -299,7 +299,13 @@
},
"RequiresHttps": {
"type": "computed",
"value": "(OrganizationalAuth || IndividualAuth)"
"value": "(OrganizationalAuth || IndividualAuth || !NoHttps)"
},
"NoHttps": {
"type": "parameter",
"datatype": "bool",
"defaultValue": "false",
"description": "Whether to turn off HTTPS. This option only applies if IndividualAuth or OrganizationalAuth are not being used."
},
"UseLocalDB": {
"type": "parameter",

View File

@ -22,7 +22,8 @@
"supportedAuthentications": [
{
"auth": "None",
"authenticationType": "NoAuth"
"authenticationType": "NoAuth",
"allowUnsecured": true
},
{
"auth": "Individual",
@ -61,5 +62,6 @@
],
"excludeLaunchSettings": false,
"azureReplyUrlPortName": "HttpsPort",
"minFullFrameworkVersion": "4.6.1"
"minFullFrameworkVersion": "4.6.1",
"disableHttpsSymbol": "NoHttps"
}

View File

@ -18,7 +18,11 @@
"Company.WebApplication1": {
"commandName": "Project",
"launchBrowser": true,
//#if(RequiresHttps)
"applicationUrl": "https://localhost:5001;http://localhost:5000",
//#else
"applicationUrl": "http://localhost:5000",
//#endif
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}

View File

@ -21,7 +21,9 @@ using Microsoft.AspNetCore.Identity;
#endif
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
#if (RequiresHttps)
using Microsoft.AspNetCore.HttpsPolicy;
#endif
using Microsoft.AspNetCore.Mvc;
#if (OrganizationalAuth)
using Microsoft.AspNetCore.Mvc.Authorization;
@ -144,10 +146,15 @@ namespace Company.WebApplication1
else
{
app.UseExceptionHandler("/Error");
#if (RequiresHttps)
app.UseHsts();
}
app.UseHttpsRedirection();
#else
}
#endif
app.UseStaticFiles();
app.UseCookiePolicy();

View File

@ -48,7 +48,7 @@ namespace Templates.Test
throw new NotImplementedException();
}
protected void RunDotNetNew(string templateName, string targetFrameworkOverride, string auth = null, string language = null, bool useLocalDB = false)
protected void RunDotNetNew(string templateName, string targetFrameworkOverride, string auth = null, string language = null, bool useLocalDB = false, bool noHttps = false)
{
var args = $"new {templateName}";
@ -72,6 +72,11 @@ namespace Templates.Test
args += $" -uld";
}
if (noHttps)
{
args += $" --no-https";
}
// Only run one instance of 'dotnet new' at once, as a workaround for
// https://github.com/aspnet/templating/issues/63
lock (DotNetNewLock)

View File

@ -14,18 +14,22 @@ namespace Templates.Test
}
[ConditionalFact]
[OSSkipCondition(OperatingSystems.Linux)]
[OSSkipCondition(OperatingSystems.MacOSX)]
[OSSkipCondition(OperatingSystems.Linux | OperatingSystems.MacOSX)]
public void RazorPagesTemplate_NoAuth_Works_NetFramework()
=> RazorPagesTemplate_NoAuthImpl("net461");
[ConditionalFact]
[OSSkipCondition(OperatingSystems.Linux | OperatingSystems.MacOSX)]
public void RazorPagesTemplate_NoAuth_NoHttps_Works_NetFramework()
=> RazorPagesTemplate_NoAuthImpl("net471", true);
[Fact]
public void RazorPagesTemplate_NoAuth_Works_NetCore()
=> RazorPagesTemplate_NoAuthImpl(null);
private void RazorPagesTemplate_NoAuthImpl(string targetFrameworkOverride)
private void RazorPagesTemplate_NoAuthImpl(string targetFrameworkOverride, bool noHttps = false)
{
RunDotNetNew("razor", targetFrameworkOverride);
RunDotNetNew("razor", targetFrameworkOverride, noHttps: noHttps);
AssertFileExists("Pages/Shared/_LoginPartial.cshtml", false);
@ -36,6 +40,18 @@ namespace Templates.Test
Assert.DoesNotContain("Microsoft.EntityFrameworkCore.Tools.DotNet", projectFileContents);
Assert.DoesNotContain("Microsoft.Extensions.SecretManager.Tools", projectFileContents);
if (targetFrameworkOverride != null)
{
if (noHttps)
{
Assert.DoesNotContain("Microsoft.AspNetCore.HttpsPolicy", projectFileContents);
}
else
{
Assert.Contains("Microsoft.AspNetCore.HttpsPolicy", projectFileContents);
}
}
foreach (var publish in new[] { false, true })
{
using (var aspNetProcess = StartAspNetProcess(targetFrameworkOverride, publish))
@ -48,11 +64,15 @@ namespace Templates.Test
}
[ConditionalFact(Skip = "https://github.com/aspnet/templating/issues/378")]
[OSSkipCondition(OperatingSystems.Linux)]
[OSSkipCondition(OperatingSystems.MacOSX)]
[OSSkipCondition(OperatingSystems.Linux | OperatingSystems.MacOSX)]
public void RazorPagesTemplate_IndividualAuth_Works_NetFramework()
=> RazorPagesTemplate_IndividualAuthImpl("net461");
[ConditionalFact(Skip = "https://github.com/aspnet/templating/issues/378")]
[OSSkipCondition(OperatingSystems.Linux | OperatingSystems.MacOSX)]
public void RazorPagesTemplate_WithIndividualAuth_NoHttpsSetToTrue_UsesHttps_NetFramework()
=> RazorPagesTemplate_IndividualAuthImpl("net471", false, true);
[Fact(Skip = "https://github.com/aspnet/templating/issues/378")]
public void RazorPagesTemplate_IndividualAuth_Works_NetCore()
=> RazorPagesTemplate_IndividualAuthImpl(null);
@ -61,7 +81,7 @@ namespace Templates.Test
public void RazorPagesTemplate_IndividualAuth_UsingLocalDB_Works_NetCore()
=> RazorPagesTemplate_IndividualAuthImpl(null, true);
private void RazorPagesTemplate_IndividualAuthImpl(string targetFrameworkOverride, bool useLocalDB = false)
private void RazorPagesTemplate_IndividualAuthImpl(string targetFrameworkOverride, bool useLocalDB = false, bool noHttps = false)
{
RunDotNetNew("razor", targetFrameworkOverride, auth: "Individual", useLocalDB: useLocalDB);
@ -74,6 +94,11 @@ namespace Templates.Test
}
Assert.Contains("Microsoft.VisualStudio.Web.CodeGeneration.Design", projectFileContents);
if (targetFrameworkOverride != null)
{
Assert.Contains("Microsoft.AspNetCore.HttpsPolicy", projectFileContents);
}
RunDotNetEfCreateMigration("razorpages");
AssertEmptyMigration("razorpages");