Skip to content

Commit f846341

Browse files
committed
Switching over to using SSL in Rider (modifying Kestrel configuration)
1 parent 67fb3ec commit f846341

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

OdeToFoodRider/OdeToFoodRider/Program.cs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
5+
using System.Net;
6+
using System.Security.Cryptography.X509Certificates;
57
using System.Threading.Tasks;
68
using Microsoft.AspNetCore;
79
using Microsoft.AspNetCore.Hosting;
@@ -14,12 +16,36 @@ public class Program
1416
{
1517
public static void Main(string[] args)
1618
{
17-
BuildWebHost(args).Run();
18-
}
19+
var config = new ConfigurationBuilder()
20+
.SetBasePath(Directory.GetCurrentDirectory())
21+
.AddEnvironmentVariables()
22+
.AddJsonFile("certificate.json", optional: true, reloadOnChange: true)
23+
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
24+
.Build();
1925

20-
public static IWebHost BuildWebHost(string[] args) =>
21-
WebHost.CreateDefaultBuilder(args)
26+
var certificateSettings = config.GetSection("certificateSettings");
27+
string certificateFileName = certificateSettings.GetValue<string>("filename");
28+
string certificatePassword = certificateSettings.GetValue<string>("password");
29+
30+
var certificate = new X509Certificate2(certificateFileName, certificatePassword);
31+
32+
var host = new WebHostBuilder()
33+
.UseKestrel(options =>
34+
{
35+
options.AddServerHeader = false;
36+
options.Listen(IPAddress.Loopback, 44321,
37+
listenOptions =>
38+
{
39+
listenOptions.UseHttps(certificate);
40+
});
41+
})
42+
.UseConfiguration(config)
43+
.UseContentRoot(Directory.GetCurrentDirectory())
2244
.UseStartup<Startup>()
45+
.UseUrls("https://localhost:44321")
2346
.Build();
47+
48+
host.Run();
49+
}
2450
}
2551
}

OdeToFoodRider/OdeToFoodRider/Startup.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using Microsoft.AspNetCore.Builder;
66
using Microsoft.AspNetCore.Hosting;
77
using Microsoft.AspNetCore.Http;
8+
using Microsoft.AspNetCore.Mvc;
9+
using Microsoft.AspNetCore.Rewrite;
810
using Microsoft.AspNetCore.Routing;
911
using Microsoft.EntityFrameworkCore;
1012
using Microsoft.Extensions.Configuration;
@@ -32,8 +34,18 @@ public void ConfigureServices(IServiceCollection services)
3234
services.AddDbContext<OdeToFoodDbContext>(options =>
3335
options.UseSqlServer(_configuration.GetConnectionString("OdeToFood")));
3436
services.AddScoped<IRestaurantData, SqlRestaurantData>();
35-
services.AddMvc();
36-
37+
services.AddMvc(options =>
38+
{
39+
options.SslPort = 44321;
40+
options.Filters.Add(new RequireHttpsAttribute());
41+
});
42+
services.AddAntiforgery(options =>
43+
{
44+
options.Cookie.Name = "_af";
45+
options.Cookie.HttpOnly = true;
46+
options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
47+
options.HeaderName = "X-XSRF-TOKEN";
48+
});
3749
}
3850

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

59+
app.UseRewriter(new RewriteOptions().AddRedirectToHttpsPermanent());
4760
app.UseStaticFiles();
4861
app.UseMvc(ConfigureRoutes);
4962

0 commit comments

Comments
 (0)