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

 更新时间:2016年07月11日 10:01:13   作者:欧阳敏岚VicBilibily  
这篇文章主要为大家详细介绍了ASP.NET Core 1.0 部署 HTTPS(.NET Core 1.0),感兴趣的小伙伴们可以参考一下

最近要做一个项目,正逢ASP.Net Core 1.0版本的正式发布。由于现代互联网的安全要求,HTTPS加密通讯已成主流,所以就有了这个方案。
本方案启发于一个旧版的解决方案:
ASP.NET Core 1.0 部署 HTTPS (.NET Framework 4.5.1)
http://www.cnblogs.com/qin-nz/p/aspnetcore-using-https-on-dnx451.html?utm_source=tuicool&utm_medium=referral
 在反复搜索官方文档并反复尝试以后得出以下解决方案
 在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");
   });

  }
 }
}

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

相关文章

  • Asp.net core 使用SignalR推送消息过程详解

    Asp.net core 使用SignalR推送消息过程详解

    ASP.NET Core SignalR 是一个开放源代码库,可用于简化向应用添加实时 Web 功能。 实时 Web 功能使服务器端代码能够将内容推送到客户端,本文重点给大家介绍Asp.net core 使用SignalR推送消息,感兴趣的朋友一起看看吧
    2022-03-03
  • 使用 Salt + Hash 将密码加密后再存储进数据库

    使用 Salt + Hash 将密码加密后再存储进数据库

    如果你需要保存密码(比如网站用户的密码),你要考虑如何保护这些密码数据,象下面那样直接将密码写入数据库中是极不安全的,因为任何可以打开数据库的人,都将可以直接看到这些密码
    2012-12-12
  • Asp.net之TextBox只允许输入数字的方法总结

    Asp.net之TextBox只允许输入数字的方法总结

    Asp.net之TextBox只允许输入数字的方法总结,需要的朋友可以参考一下
    2013-02-02
  • C#中OpenFileDialog和PictrueBox的用法分析

    C#中OpenFileDialog和PictrueBox的用法分析

    这篇文章主要介绍了C#中OpenFileDialog和PictrueBox的用法,以实例的形式较为详细的分析了OpenFileDialog和PictrueBox使用时的注意事项与具体用法,具有一定的参考借鉴价值,需要的朋友可以参考下
    2014-11-11
  • ASP.NET Core优雅的在开发环境保存机密(User Secrets)

    ASP.NET Core优雅的在开发环境保存机密(User Secrets)

    这篇文章主要为大家详细介绍了ASP.NET Core如何优雅的在开发环境保存机密User Secrets,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-05-05
  • NetCore WebSocket即时通讯示例

    NetCore WebSocket即时通讯示例

    这篇文章主要为大家详细介绍了NetCore WebSocket即时通讯示例,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • asp.net core实体类生产CRUD后台管理界面

    asp.net core实体类生产CRUD后台管理界面

    这篇文章主要为大家介绍了asp.net core实体类生产CRUD后台管理界面详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-05-05
  • ASP.NET操作Excel备忘录

    ASP.NET操作Excel备忘录

    ASP.NET下操作Excel的一些技巧分析,需要的朋友可以参考下。
    2010-01-01
  • asp.net core configuration配置读取的实现

    asp.net core configuration配置读取的实现

    本文主要介绍了asp.net core configuration配置读取,configuration可以从命令行、环境变量、配置文件读取配置,具有一定的参考价值,感兴趣的可以了解一下
    2023-11-11
  • MVC4制作网站教程第三章 修改用户组操作3.3

    MVC4制作网站教程第三章 修改用户组操作3.3

    这篇文章主要为大家详细介绍了MVC4制作网站教程,修改用户组功能的实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-08-08

最新评论