asp.net core 1 Department HTTPS (. NET Core 1.0) _ Practical Tips

Source: Internet
Author: User
Tags dotnet hosting


A recent project is being launched at the ASP.net Core 1.0 release. Because of the modern Internet security requirements, HTTPS encrypted communication has become mainstream, so there is this scheme.
This scenario is inspired by an older version of the solution:
asp.net Core 1 Department 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
  After searching the official document repeatedly and trying to come up with the following solution
  in Project.json, add 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.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": {
 //cross-platform reference
 //"netcoreapp1.0": {
 // "imports": [
 // "dotnet5.6",
 // "portable-net45+win8"
 // ]
 //}
 / / Windows platform universal reference
 "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%" ]
 }
} 


In Program.cs, increase the HTTPS access port bindings


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 Configuration Builder (). 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 it 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 is gets called by the runtime.
  Use the 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 => {return run.
   Response.writeasync ("Test");

  });
 }
 }
}


The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.


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.