Deploying HTTPS (. NET Core 1.0) and corehttps in ASP. NET Core 1.0
One project is to be created in the past two months, and the official release of ASP. Net Core 1.0 is expected. Due to the security requirements of the modern Internet, HTTPS encrypted communication has become the mainstream, so this solution is available.
This solution is inspired by an old version of solution:
Deploying HTTPS (. NET Framework 4.5.1) in ASP. NET Core 1.0)
Http://www.cnblogs.com/qin-nz/p/aspnetcore-using-https-on-dnx451.html? Utm_source = tuicool & utm_medium = referral
Find the following solutions after searching the official documents and making repeated attempts:
Inproject.jsonAdd reference Microsoft. AspNetCore. Server. Kestrel. Https
1 {2 "dependencies": {3 // cross-platform reference 4 // "Microsoft. NETCore. app ": {5 //" version ":" 1.0.0 ", 6 //" type ":" platform "7 //}, 8" Microsoft. aspNetCore. diagnostics ":" 1.0.0 ", 9" Microsoft. aspNetCore. mvc ":" 1.0.0 ", 10" Microsoft. aspNetCore. razor. tools ": {11" version ":" 1.0.0-preview2-final ", 12" type ":" build "13}, 14" Microsoft. aspNetCore. server. IISIntegration ":" 1.0.0 ", 15" Microsoft. aspNetCore. server. kestrel ":" 1.0.0 ", 16" Microsoft. aspNetCore. server. kestrel. https ":" 1.0.0 ", 17" Microsoft. aspNetCore. staticFiles ":" 1.0.0 ", 18" Microsoft. extensions. configuration. environmentVariables ":" 1.0.0 ", 19" Microsoft. extensions. configuration. json ":" 1.0.0 ", 20" Microsoft. extensions. logging ":" 1.0.0 ", 21" Microsoft. extensions. logging. console ":" 1.0.0 ", 22" Microsoft. extensions. logging. debug ":" 1.0.0 ", 23" Microsoft. extensions. options. configurationExtensions ":" 1.0.0 ", 24" Microsoft. visual Studio. web. browserLink. loader ":" 14.0.0 "25}, 26 27" tools ": {28" BundlerMinifier. core ":" 2.0.238 ", 29" Microsoft. aspNetCore. razor. tools ":" 1.0.0-preview2-final ", 30" Microsoft. aspNetCore. server. IISIntegration. tools ":" 1.0.0-preview2-final "31}, 32 33" frameworks ": {34 // cross-platform reference 35 //" netcoreapp1.0 ": {36 //" imports ": [37 // "dotnet5.6", 38 // "portable-net45 + win8" 39 //] 40 //} 41 // Windows platform generic reference 42 "net452 ": {} 43}, 44 45 "buildOptions": {46 "emitEntryPoint": true, 47 "preserveCompilationContext": true48}, 49 50 "runtimeOptions": {51 "configProperties ": {52 "System. GC. server ": true53} 54}, 55 56" publishOptions ": {57" include ": [58" wwwroot ", 59" Views ", 60 "Areas/**/Views", 61 "regionettings. json ", 62" web. config "63], 64" exclude ": [65" wwwroot/lib "66] 67}, 68 69" scripts ": {70" prepublish ": [" bower install ", "dotnet bundle"], 71 "postpublish": ["dotnet publish-iis -- publish-folder % publish: OutputPath % -- framework % publish: FullTargetFramework %"] 72} 73}Project. json
Add HTTPS access port binding in Program. cs
1 using System; 2 using System. collections. generic; 3 using System. IO; 4 using System. linq; 5 using System. threading. tasks; 6 using Microsoft. aspNetCore. hosting; 7 8 namespace Demo 9 {10 public class Program11 {12 public static void Main (string [] args) 13 {14 15 var host = new WebHostBuilder () 16. useKestrel () 17. useUrls ("http: // *", "https: // *") 18. useContentRoot (Directory. getCurrentDirectory () 19. useIISIntegration () 20. useStartup <Startup> () 21. build (); 22 23 host. run (); 24} 25} 26}Program. cs
InStartup.csFile, enable HTTPS access and configure the certificate path and password
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Threading.Tasks;
5 using Microsoft.AspNetCore.Builder;
6 using Microsoft.AspNetCore.Hosting;
7 using Microsoft.Extensions.Configuration;
8 using Microsoft.Extensions.DependencyInjection;
9 using Microsoft.Extensions.Logging;
10 using System.IO;
11 using Microsoft.AspNetCore.Http;
12
13 namespace Demo
14 {
15 public class Startup
16 {
17 public Startup (IHostingEnvironment env)
18 {
19 var builder = new ConfigurationBuilder ()
20 .SetBasePath (env.ContentRootPath)
21 .AddJsonFile ("appsettings.json", optional: true, reloadOnChange: true)
22. .AddJsonFile ($ "appsettings. {Env.EnvironmentName} .json", optional: true)
23 .AddEnvironmentVariables ();
24 Configuration = builder.Build ();
25}
26
27 public IConfigurationRoot Configuration {get;}
28
29 // This method gets called by the runtime. Use this method to add services to the container.
30 public void ConfigureServices (IServiceCollection services)
31 {
32
33 // Add framework services.
34 services.AddMvc ();
35
36 services.Configure <Microsoft.AspNetCore.Server.Kestrel.KestrelServerOptions> (option => {
37 option.UseHttps (Path.Combine (new DirectoryInfo (Directory.GetCurrentDirectory ()). FullName, "cret.pfx"), "pw");
38});
39
40
41
42}
43
44 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
45 public void Configure (IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
46 {
47 loggerFactory.AddConsole (Configuration.GetSection ("Logging"));
48 loggerFactory.AddDebug ();
49
50 if (env.IsDevelopment ())
51 {
52 app.UseDeveloperExceptionPage ();
53 app.UseBrowserLink ();
54}
55 else
56 {
57 app.UseExceptionHandler ("/ Home / Error");
58}
59
60
61 app.UseStaticFiles ();
62
63 app.UseMvc (routes =>
64 {
65 routes.MapRoute (
66 name: "default",
67 template: "{controller = App} / {action = Index} / {id?}");
68});
69
70 //https://docs.asp.net/en/latest/security/cors.html?highlight=https
71 app.UseCors (builder => builder.WithOrigins ("https: // *") .AllowAnyHeader ());
72
73 app.Run (run =>
74 {
75 return run.Response.WriteAsync ("Test");
76});
77
78}
79}
80}