ASP.NET Core 1.0 部署 HTTPS (.NET Core 1.0)
程序员文章站
2022-06-07 20:34:40
这两个月要做一个项目,正逢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
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访问端口绑定
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 }
推荐阅读
-
ASP.NET Core部署前期准备 使用Hyper-V安装Ubuntu Server 16.10
-
win10下ASP.NET Core部署环境搭建步骤
-
ubuntu16.4下用jexus部署ASP.NET Core环境
-
ASP.NET Core 1.0实现邮件发送功能
-
.net core 1.0 实现单点登录负载多服务器
-
详解ASP.NET Core部署项目到Ubuntu Server
-
详解ASP.NET Core部署项目到Ubuntu Server
-
docker部署Asp.net core应用的完整步骤
-
ASP.Net Core on Linux (CentOS7) 共享第三方依赖库部署
-
ASP.NET Core 3.0 : 二十八. 在Docker中的部署以及docker-compose的使用