欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET Core 1.0 部署 HTTPS (.NET Core 1.0)

程序员文章站 2023-10-24 13:33:45
这两个月要做一个项目,正逢asp.net core 1.0版本的正式发布。由于现代互联网的安全要求,https通讯已成主流,所以就有了这个方案。 本方案启发于一个旧版的解决方案: asp.net c...

这两个月要做一个项目,正逢asp.net core 1.0版本的正式发布。由于现代互联网的安全要求,https通讯已成主流,所以就有了这个方案。

本方案启发于一个旧版的解决方案:

asp.net core 1.0 部署 https (.net framework 4.5.1)

https://www.cnblogs.com/qin-nz/p/netcore-using-https-on-dnx451.html?utm_source=tuicool&utm_medium=referral

在反复搜索官方文档并反复尝试以后得出以下解决方案

 

project.json 中,添加引用 microsoft.aspnetcore.server.kestrel.https

ASP.NET Core 1.0 部署 HTTPS (.NET Core 1.0)ASP.NET Core 1.0 部署 HTTPS (.NET Core 1.0)
 1 {
 2   "dependencies": {
 3     //跨平台引用
 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.visualstudio.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     //跨平台引用
35     //"netcoreapp1.0": {
36     //  "imports": [
37     //    "dotnet5.6",
38     //    "portable-net45+win8"
39     //  ]
40     //}
41     //windows平台通用化引用
42     "net452": {}
43   },
44 
45   "buildoptions": {
46     "emitentrypoint": true,
47     "preservecompilationcontext": true
48   },
49 
50   "runtimeoptions": {
51     "configproperties": {
52       "system.gc.server": true
53     }
54   },
55 
56   "publishoptions": {
57     "include": [
58       "wwwroot",
59       "views",
60       "areas/**/views",
61       "appsettings.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

在program.cs中,增加https访问端口绑定

ASP.NET Core 1.0 部署 HTTPS (.NET Core 1.0)ASP.NET Core 1.0 部署 HTTPS (.NET Core 1.0)
 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 program
11     {
12         public static void main(string[] args)
13         {
14 
15             var host = new webhostbuilder()
16                 .usekestrel()
17                 .useurls("https://*", "https://*")
18                 .usecontentroot(directory.getcurrentdirectory())
19                 .useiisintegration()
20                 .usestartup()
21                 .build();
22 
23             host.run();
24         }
25     }
26 }
program.cs

startup.cs 文件中,启用https访问并配置证书路径及密码

 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(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 }