投げ銭

★当サイトへの投げ銭(PayPal)★

LINK


(無償、寄付歓迎) logo
世界中で使われるISO標準オフィスソフト(MSオフィス互換)

★LibreOfficeの導入事例★
詳細について

人気の投稿(1ヶ月間)

Ad

Ad

投げ銭

★当サイトへの投げ銭(PayPal)★

2020年8月28日金曜日

【ASP.NET Core 3.1.102】他マシンのブラウザからでもhttpでWEBページにアクセスできるようにする【Linux】

asp.net core 3で、テストプロジェクト(myproject)を作成した。
しかしプロジェクトのフォルダにて dotnet run して、他のマシンのブラウザからIPアドレスを入力したがWEBページが表示できなかった。

プログラムが動作しているマシン上のブラウザからなら、アクセスが可能だった。
一種のアクセス制御がデフォルトで機能しているためだとわかった。
調べてみるとデフォルトでは、localhost(127.0.0.1)でのみListenしているようだった。

他のマシンのブラウザからもアクセス可能なようにするには、イントラネットからアクセスできるその他のIPアドレスでもListenするように設定する必要があった。


そして、次の手順の設定することによって外部マシンからでも、テストプログラムにhttpあるいは、httpsアクセスが可能になった。

httpsでなく、httpでアクセスしたい場合は、手順1だけでなく、手順2の設定も必要になる。



1、launchSettings.json で、applicationUrl でListenするIPアドレスとポートを指定する。

$ cat Properties/launchSettings.json

{
  "iisSettings": {
    "windowsAuthentication": false, 
    "anonymousAuthentication": true, 
    "iisExpress": {
      "applicationUrl": "http://localhost:9291",
      "sslPort": 44347
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "myproject": {
      "commandName": "Project",
      "launchBrowser": true,
      "applicationUrl": "https://192.168.12.13:5001;http://192.168.12.13:5000;https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}


上記設定で、https://192.168.12.13:5001;http://192.168.12.13:5000 のように、httpsも併せて設定している場合は、httpsでも接続できるのでアクセスできなくなる問題は生じない。

しかし、httpのみ設定した場合は、デフォルトではうまくアクセスできなかった。
これは、httpsでアクセスするように自動でリダイレクトされるからだ。

httpでアクセスしたい場合は、手順1に加えて、手順2のようにしてhttpsでのリダイレクトを行わないように設定する必要がある。



2、app.UseHttpsRedirection(); をコメントアウトする。(必要に応じて)

app.UseHttpsRedirection() が設定されている場合、httpでアクセスしても、httpsでアクセスするようにリダイレクトされてしまう。
そこで、httpでもアクセスできるように、下記のようにコメントアウトしておく。


namespace surveilcam
{
    public class Startup
    {
(略)

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
(略)
            //app.UseHttpsRedirection();
(略)



以上で、設定は完了である。
dotnet run --project myproject で動作させ、ブラウザからhttpでアクセスすることができた。

$ dotnet run --project myproject
info: Microsoft.Hosting.Lifetime[0]
      Now listening on: https://192.168.12.13:4001
info: Microsoft.Hosting.Lifetime[0]
      Now listening on: http://192.168.12.13:4000
info: Microsoft.Hosting.Lifetime[0]
      Now listening on: https://localhost:4001
info: Microsoft.Hosting.Lifetime[0]
      Now listening on: http://localhost:4000
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
      Content root path: /home/pi/dotnet/razortest
info: Microsoft.Hosting.Lifetime[0]



<参考>
・5 ways to set the URLs for an ASP.NET Core app
< https://andrewlock.net/5-ways-to-set-the-urls-for-an-aspnetcore-app/ > 2020年8月28日

投げ銭

★当サイトへの投げ銭(PayPal)★

Ad

Ad