Skip to content

Commit

Permalink
Switching over to using SSL in Rider (modifying Kestrel configuration)
Browse files Browse the repository at this point in the history
  • Loading branch information
gorohoroh committed Jul 14, 2018
1 parent 67fb3ec commit f846341
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
34 changes: 30 additions & 4 deletions OdeToFoodRider/OdeToFoodRider/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
Expand All @@ -14,12 +16,36 @@ public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddEnvironmentVariables()
.AddJsonFile("certificate.json", optional: true, reloadOnChange: true)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();

public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
var certificateSettings = config.GetSection("certificateSettings");
string certificateFileName = certificateSettings.GetValue<string>("filename");
string certificatePassword = certificateSettings.GetValue<string>("password");

var certificate = new X509Certificate2(certificateFileName, certificatePassword);

var host = new WebHostBuilder()
.UseKestrel(options =>
{
options.AddServerHeader = false;
options.Listen(IPAddress.Loopback, 44321,
listenOptions =>
{
listenOptions.UseHttps(certificate);
});
})
.UseConfiguration(config)
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.UseUrls("https://localhost:44321")
.Build();

host.Run();
}
}
}
17 changes: 15 additions & 2 deletions OdeToFoodRider/OdeToFoodRider/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Rewrite;
using Microsoft.AspNetCore.Routing;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
Expand Down Expand Up @@ -32,8 +34,18 @@ public void ConfigureServices(IServiceCollection services)
services.AddDbContext<OdeToFoodDbContext>(options =>
options.UseSqlServer(_configuration.GetConnectionString("OdeToFood")));
services.AddScoped<IRestaurantData, SqlRestaurantData>();
services.AddMvc();

services.AddMvc(options =>
{
options.SslPort = 44321;
options.Filters.Add(new RequireHttpsAttribute());
});
services.AddAntiforgery(options =>
{
options.Cookie.Name = "_af";
options.Cookie.HttpOnly = true;
options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
options.HeaderName = "X-XSRF-TOKEN";
});
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand All @@ -44,6 +56,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, IGreeter
app.UseDeveloperExceptionPage();
}

app.UseRewriter(new RewriteOptions().AddRedirectToHttpsPermanent());
app.UseStaticFiles();
app.UseMvc(ConfigureRoutes);

Expand Down

0 comments on commit f846341

Please sign in to comment.