当前位置:Gxlcms > asp.net > ASP.NET Core 1.0 部署 HTTPS(.NET Core 1.0)

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

时间:2021-07-01 10:21:17 帮助过:24人阅读

最近要做一个项目,正逢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

  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. "tools": {
  27. "BundlerMinifier.Core": "2.0.238",
  28. "Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final",
  29. "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
  30. },
  31. "frameworks": {
  32. //跨平台引用
  33. //"netcoreapp1.0": {
  34. // "imports": [
  35. // "dotnet5.6",
  36. // "portable-net45+win8"
  37. // ]
  38. //}
  39. //Windows平台通用化引用
  40. "net452": {}
  41. },
  42. "buildOptions": {
  43. "emitEntryPoint": true,
  44. "preserveCompilationContext": true
  45. },
  46. "runtimeOptions": {
  47. "configProperties": {
  48. "System.GC.Server": true
  49. }
  50. },
  51. "publishOptions": {
  52. "include": [
  53. "wwwroot",
  54. "Views",
  55. "Areas/**/Views",
  56. "appsettings.json",
  57. "web.config"
  58. ],
  59. "exclude": [
  60. "wwwroot/lib"
  61. ]
  62. },
  63. "scripts": {
  64. "prepublish": [ "bower install", "dotnet bundle" ],
  65. "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  66. }
  67. }

在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. namespace Demo
  8. {
  9. public class Program
  10. {
  11. public static void Main(string[] args)
  12. {
  13. var host = new WebHostBuilder()
  14. .UseKestrel()
  15. .UseUrls("http://*", "https://*")
  16. .UseContentRoot(Directory.GetCurrentDirectory())
  17. .UseIISIntegration()
  18. .UseStartup<Startup>()
  19. .Build();
  20. host.Run();
  21. }
  22. }
  23. }

在 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. namespace Demo
  13. {
  14. public class Startup
  15. {
  16. public Startup(IHostingEnvironment env)
  17. {
  18. var builder = new ConfigurationBuilder()
  19. .SetBasePath(env.ContentRootPath)
  20. .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
  21. .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
  22. .AddEnvironmentVariables();
  23. Configuration = builder.Build();
  24. }
  25. public IConfigurationRoot Configuration { get; }
  26. // This method gets called by the runtime. Use this method to add services to the container.
  27. public void ConfigureServices(IServiceCollection services)
  28. {
  29. // Add framework services.
  30. services.AddMvc();
  31. services.Configure<Microsoft.AspNetCore.Server.Kestrel.KestrelServerOptions>(option => {
  32. option.UseHttps(Path.Combine(new DirectoryInfo(Directory.GetCurrentDirectory()).FullName, "cret.pfx"), "pw");
  33. });
  34. }
  35. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  36. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
  37. {
  38. loggerFactory.AddConsole(Configuration.GetSection("Logging"));
  39. loggerFactory.AddDebug();
  40. if (env.IsDevelopment())
  41. {
  42. app.UseDeveloperExceptionPage();
  43. app.UseBrowserLink();
  44. }
  45. else
  46. {
  47. app.UseExceptionHandler("/Home/Error");
  48. }
  49. app.UseStaticFiles();
  50. app.UseMvc(routes =>
  51. {
  52. routes.MapRoute(
  53. name: "default",
  54. template: "{controller=App}/{action=Index}/{id?}");
  55. });
  56. //https://docs.asp.net/en/latest/security/cors.html?highlight=https
  57. app.UseCors(builder =>builder.WithOrigins("https://*").AllowAnyHeader());
  58. app.Run(run =>
  59. {
  60. return run.Response.WriteAsync("Test");
  61. });
  62. }
  63. }
  64. }

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

人气教程排行