Deploying HTTPS (. NET Core 1.0) and corehttps in ASP. NET Core 1.0

Source: Internet
Author: User
Tags dotnet

Deploying HTTPS (. NET Core 1.0) and corehttps in ASP. NET Core 1.0

Recently, we have to create a project, which meets the official release of ASP. Net Core 1.0. Due to the security requirements of the modern Internet, HTTPS encrypted communication has become the mainstream, so this solution is available.
This solution is inspired by an old version of solution:
Deploying HTTPS (. NET Framework 4.5.1) in ASP. NET Core 1.0)
Http://www.cnblogs.com/qin-nz/p/aspnetcore-using-https-on-dnx451.html? Utm_source = tuicool & utm_medium = referral
Find the following solutions after searching the official documents and making repeated attempts:
In project. json, add the reference Microsoft. AspNetCore. Server. Kestrel. Https

{"Dependencies": {// cross-platform reference // "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. visual Studio. 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 ": {// cross-platform reference //" netcoreapp1.0 ": {//" imports ": [//" dotnet5.6 ", // "portable-net45 + win8" //] // Windows platform generic reference "net452" :{}}, "buildOptions": {"emitEntryPoint": true, "preserveCompilationContext": true}, "runtimeOptions": {"configProperties": {"System. GC. server ": true }}," publishOptions ": {" include ": [" wwwroot "," Views "," Areas/**/Views "," etettings. json "," web. config "]," exclude ": [" wwwroot/lib "]}," scripts ": {" prepublish ": [" bower install "," dotnet bundle "], "postpublish": ["dotnet publish-iis -- publish-folder % publish: OutputPath % -- framework % publish: FullTargetFramework %"]}

Add HTTPS access port binding in Program. cs

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

In the Startup. cs file, enable HTTPS access and configure the certificate path and password.

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");   });  } }}

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.