6
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ASP.NET Core - SSLを有効化する

Last updated at Posted at 2017-06-19

開発環境

  • .NETCoreApp 1.1
  • Windows10 Pro
  • Visual Studio Community 2017
  • IIS Express

Linuxの場合

セットアップ

まず,認証機能を有効にしたMVCテンプレートを作製します.
(参考: http://qiita.com/musubi05/items/a90954d08aff5ea5369c)

SSLの有効化

Properties/launchSettings.jsonを開き,SSLのポートを開きます.
"sslPort"の項目を,44300から44399中の好きなポート番号に変更します.

launchSettings.json
{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:49455/",
      "sslPort": 44385
    }
  },
  "profiles": {
    ~~略~~
  }
}

これで,https://localhost:44385/ にアクセスできるようになります.

HTTPSへ自動リダイレクトする

Startup.csのConfigureServicesメソッド内に記載されている,services.AddMvc();を次のように記載します.
すると,httpポートでのアクセスが,すべてhttpsにリダイレクトされるようになります.

Startup.cs
using Microsoft.AspNetCore.Mvc; // RequireHttpsAttributeを参照するために必要

public void ConfigureServices(IServiceCollection services)
{
    // 中略
    services.AddMvc(options =>
    {
        options.SslPort = 44385;
        options.Filters.Add(new RequireHttpsAttribute ());
    });
    // 中略
}

参考

6
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?