当前位置: 移动技术网 > IT编程>开发语言>.net > ASP.NET Core 1.0 部署 HTTPS(.NET Core 1.0)

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

2017年12月12日  | 移动技术网IT编程  | 我要评论

傻后也让君倾心,成年人小游戏,2017斯诺克世锦赛决赛

最近要做一个项目,正逢asp.net core 1.0版本的正式发布。由于现代互联网的安全要求,https加密通讯已成主流,所以就有了这个方案。
本方案启发于一个旧版的解决方案:
asp.net core 1.0 部署 https (.net framework 4.5.1)

 在反复搜索官方文档并反复尝试以后得出以下解决方案
 在project.json 中,添加引用 microsoft.aspnetcore.server.kestrel.https

{
 "dependencies": {
 //跨平台引用
 //"microsoft.netcore.app": {
 // "version": "1.0.0",
 // "type": "platform"
 //},
 "microsoft.aspnetcore.diagnostics": "1.0.0",
 "microsoft.aspnetcore.mvc": "1.0.0",
 "microsoft.aspnetcore.razor.tools": {
  "version": "1.0.0-preview2-final",
  "type": "build"
 },
 "microsoft.aspnetcore.server.iisintegration": "1.0.0",
 "microsoft.aspnetcore.server.kestrel": "1.0.0",
 "microsoft.aspnetcore.server.kestrel.https": "1.0.0",
 "microsoft.aspnetcore.staticfiles": "1.0.0",
 "microsoft.extensions.configuration.environmentvariables": "1.0.0",
 "microsoft.extensions.configuration.json": "1.0.0",
 "microsoft.extensions.logging": "1.0.0",
 "microsoft.extensions.logging.console": "1.0.0",
 "microsoft.extensions.logging.debug": "1.0.0",
 "microsoft.extensions.options.configurationextensions": "1.0.0",
 "microsoft.visualstudio.web.browserlink.loader": "14.0.0"
 },

 "tools": {
 "bundlerminifier.core": "2.0.238",
 "microsoft.aspnetcore.razor.tools": "1.0.0-preview2-final",
 "microsoft.aspnetcore.server.iisintegration.tools": "1.0.0-preview2-final"
 },

 "frameworks": {
 //跨平台引用
 //"netcoreapp1.0": {
 // "imports": [
 // "dotnet5.6",
 // "portable-net45+win8"
 // ]
 //}
 //windows平台通用化引用
 "net452": {}
 },

 "buildoptions": {
 "emitentrypoint": true,
 "preservecompilationcontext": true
 },

 "runtimeoptions": {
 "configproperties": {
  "system.gc.server": true
 }
 },

 "publishoptions": {
 "include": [
  "wwwroot",
  "views",
  "areas/**/views",
  "appsettings.json",
  "web.config"
 ],
 "exclude": [
  "wwwroot/lib"
 ]
 },

 "scripts": {
 "prepublish": [ "bower install", "dotnet bundle" ],
 "postpublish": [ "dotnet publish-iis --publish-folder %publish:outputpath% --framework %publish:fulltargetframework%" ]
 }
}

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

using system;
using system.collections.generic;
using system.io;
using system.linq;
using system.threading.tasks;
using microsoft.aspnetcore.hosting;

namespace demo
{
 public class program
 {
  public static void main(string[] args)
  {

   var host = new webhostbuilder()
    .usekestrel()
    .useurls("http://*", "https://*")
    .usecontentroot(directory.getcurrentdirectory())
    .useiisintegration()
    .usestartup<startup>()
    .build();

   host.run();
  }
 }
}

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

using system;
using system.collections.generic;
using system.linq;
using system.threading.tasks;
using microsoft.aspnetcore.builder;
using microsoft.aspnetcore.hosting;
using microsoft.extensions.configuration;
using microsoft.extensions.dependencyinjection;
using microsoft.extensions.logging;
using system.io;
using microsoft.aspnetcore.http;

namespace demo
{
 public class startup
 {
  public startup(ihostingenvironment env)
  {
   var builder = new configurationbuilder()
    .setbasepath(env.contentrootpath)
    .addjsonfile("appsettings.json", optional: true, reloadonchange: true)
    .addjsonfile($"appsettings.{env.environmentname}.json", optional: true)
    .addenvironmentvariables();
   configuration = builder.build();
  }

  public iconfigurationroot configuration { get; }

  // this method gets called by the runtime. use this method to add services to the container.
  public void configureservices(iservicecollection services)
  {

   // add framework services.
   services.addmvc();

   services.configure<microsoft.aspnetcore.server.kestrel.kestrelserveroptions>(option => {
    option.usehttps(path.combine(new directoryinfo(directory.getcurrentdirectory()).fullname, "cret.pfx"), "pw");
   });



  }

  // this method gets called by the runtime. use this method to configure the http request pipeline.
  public void configure(iapplicationbuilder app, ihostingenvironment env, iloggerfactory loggerfactory)
  {
   loggerfactory.addconsole(configuration.getsection("logging"));
   loggerfactory.adddebug();

   if (env.isdevelopment())
   {
    app.usedeveloperexceptionpage();
    app.usebrowserlink();
   }
   else
   {
    app.useexceptionhandler("/home/error");
   }


   app.usestaticfiles();

   app.usemvc(routes =>
   {
    routes.maproute(
     name: "default",
     template: "{controller=app}/{action=index}/{id?}");
   });

   //https://docs.asp.net/en/latest/security/cors.html?highlight=https
   app.usecors(builder =>builder.withorigins("https://*").allowanyheader());

   app.run(run =>
   {
    return run.response.writeasync("test");
   });

  }
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网