質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Node.js

Node.jsとはGoogleのV8 JavaScriptエンジンを使用しているサーバーサイドのイベント駆動型プログラムです。

web.config

web.configはASP.NETウェブアプリケーションの主な設定や構成ファイルを格納するXMLファイルです。

Webサイト

一つのドメイン上に存在するWebページの集合体をWebサイトと呼びます。

Active Directory

Active Directoryは、 Windows Serverの機能の一つで、 マイクロソフトによって作られたディレクトリサービスです。 ネットワーク上に存在する様々なハードや利用者情報のアクセス権限などを一元管理が出来ます。

Azure

Azureは、マイクロソフトのクラウド プラットフォームで、旧称は Windows Azureです。PaaSとIaaSを組み合わせることで、 コンピューティング・ストレージ・データ・ネットワーキング・アプリケーションなど多くの機能を持ちます。

Q&A

0回答

1511閲覧

node.js Webサイトでページごとにアクセス制限をしたい

hp-_-

総合スコア0

Node.js

Node.jsとはGoogleのV8 JavaScriptエンジンを使用しているサーバーサイドのイベント駆動型プログラムです。

web.config

web.configはASP.NETウェブアプリケーションの主な設定や構成ファイルを格納するXMLファイルです。

Webサイト

一つのドメイン上に存在するWebページの集合体をWebサイトと呼びます。

Active Directory

Active Directoryは、 Windows Serverの機能の一つで、 マイクロソフトによって作られたディレクトリサービスです。 ネットワーク上に存在する様々なハードや利用者情報のアクセス権限などを一元管理が出来ます。

Azure

Azureは、マイクロソフトのクラウド プラットフォームで、旧称は Windows Azureです。PaaSとIaaSを組み合わせることで、 コンピューティング・ストレージ・データ・ネットワーキング・アプリケーションなど多くの機能を持ちます。

0グッド

0クリップ

投稿2020/09/25 08:07

前提・実現したいこと

node.js で作成した Web サイトをページごとにアクセス制限したいです。
Web.configで設定する方法を試しましたが、正しい方法、またはそれ以外の方法があれば教えてください。

【現状】
Azure App Service に node.js で Web ページを作りました。
オペレーティングシステムは Windows を選びました。
Azure AD B2C(local account)で認証してこのWebサイトにアクセスできるようになっています。

ファイル構成は以下のようになっています。

  • index.html(メインのhtml)
  • sub.html(サブのhtml)
  • server.js (メインのjs)
  • pulic/styles.css
  • Web.config

index.htmlには認証が通った全ユーザーがアクセスできるようにし、
sub.htmlには認証が通ったユーザーの中でも、一部のユーザーだけがアクセスできるようにしたいと思っています。
一部のユーザーはメールアドレスのドメインで仕分けをします。
例えば、***@gmail.comでアクセスした人はsub.htmlを閲覧でき、
***@outlook.comでアクセスした人はsub.htmlを閲覧しようとすると、「あなたにはアクセス権がありません」と表示されるページにリダイレクトされるようにします。
なお、リダイレクトの方は今のところ試しておりません。

Web.config 初期設定のコード

まず、ページごとにアクセス制限をかける前の初期のWeb.configに以下のコードを書きました。
コードはこちらを参考にしました。

この状態だとサイトは問題なく表示されます。

Web.config

1<?xml version="1.0" encoding="utf-8"?> 2<!-- 3 This configuration file is required if iisnode is used to run node processes behind 4 IIS or IIS Express. For more information, visit: 5 6 https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config 7--> 8 9<configuration> 10 <system.webServer> 11 <!-- Visit http://blogs.msdn.com/b/windowsazure/archive/2013/11/14/introduction-to-websockets-on-windows-azure-web-sites.aspx for more information on WebSocket support --> 12 <webSocket enabled="false" /> 13 <handlers> 14 <!-- Indicates that the server.js file is a node.js site to be handled by the iisnode module --> 15 <add name="iisnode" path="server.js" verb="*" modules="iisnode"/> 16 </handlers> 17 <rewrite> 18 <rules> 19 <!-- Do not interfere with requests for node-inspector debugging --> 20 <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true"> 21 <match url="^server.js/debug[/]?" /> 22 </rule> 23 24 <!-- First we consider whether the incoming URL matches a physical file in the /public folder --> 25 <rule name="StaticContent"> 26 <action type="Rewrite" url="public{REQUEST_URI}"/> 27 </rule> 28 29 <!-- All other URLs are mapped to the node.js site entry point --> 30 <rule name="DynamicContent"> 31 <conditions> 32 <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/> 33 </conditions> 34 <action type="Rewrite" url="server.js"/> 35 </rule> 36 </rules> 37 </rewrite> 38 39 <!-- 'bin' directory has no special meaning in node.js and apps can be placed in it --> 40 <security> 41 <requestFiltering> 42 <hiddenSegments> 43 <remove segment="bin"/> 44 </hiddenSegments> 45 </requestFiltering> 46 </security> 47 48 <!-- Make sure error responses are left untouched --> 49 <httpErrors existingResponse="PassThrough" /> 50 51 <!-- 52 You can control how Node is hosted within IIS using the following options: 53 * watchedFiles: semi-colon separated list of files that will be watched for changes to restart the server 54 * node_env: will be propagated to node as NODE_ENV environment variable 55 * debuggingEnabled - controls whether the built-in debugger is enabled 56 57 See https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config for a full list of options 58 --> 59 <!--<iisnode watchedFiles="web.config;*.js"/>--> 60 </system.webServer> 61</configuration>

該当のソースコード

初期の Web.config に以下のコードを追加しました。
コードはこちら①こちら②を参考にしました。

Web.config

1<?xml version="1.0" encoding="utf-8"?> 2<configuration> 3 <location path="sub.html"> 4 <system.webServer> 5 <authorization> 6 <allow users="*@gmail.com"/> 7 <deny users="*"/> 8 </authorization> 9 </system.webServer> 10 </location> 11</configuration>

なお、参照先のマイクロソフトの公式ドキュメントには以下の文章がありましたので、最終的にはこのようなコードにしました。
Web.config が既に存在する場合は、すべての内容を含めた <authorization> 要素を追加するだけです。 許可するアカウントを <allow> 要素に追加します。

Web.config

1<?xml version="1.0" encoding="utf-8"?> 2<!-- 3 This configuration file is required if iisnode is used to run node processes behind 4 IIS or IIS Express. For more information, visit: 5 6 https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config 7--> 8 9<configuration> 10 <system.webServer> 11 <!-- Visit http://blogs.msdn.com/b/windowsazure/archive/2013/11/14/introduction-to-websockets-on-windows-azure-web-sites.aspx for more information on WebSocket support --> 12 <webSocket enabled="false" /> 13 <handlers> 14 <!-- Indicates that the server.js file is a node.js site to be handled by the iisnode module --> 15 <add name="iisnode" path="server.js" verb="*" modules="iisnode"/> 16 </handlers> 17 <rewrite> 18 <rules> 19 <!-- Do not interfere with requests for node-inspector debugging --> 20 <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true"> 21 <match url="^server.js/debug[/]?" /> 22 </rule> 23 24 <!-- First we consider whether the incoming URL matches a physical file in the /public folder --> 25 <rule name="StaticContent"> 26 <action type="Rewrite" url="public{REQUEST_URI}"/> 27 </rule> 28 29 <!-- All other URLs are mapped to the node.js site entry point --> 30 <rule name="DynamicContent"> 31 <conditions> 32 <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/> 33 </conditions> 34 <action type="Rewrite" url="server.js"/> 35 </rule> 36 </rules> 37 </rewrite> 38 39 <!-- 'bin' directory has no special meaning in node.js and apps can be placed in it --> 40 <security> 41 <requestFiltering> 42 <hiddenSegments> 43 <remove segment="bin"/> 44 </hiddenSegments> 45 </requestFiltering> 46 </security> 47 48 <!-- Make sure error responses are left untouched --> 49 <httpErrors existingResponse="PassThrough" /> 50 51 <!-- 52 You can control how Node is hosted within IIS using the following options: 53 * watchedFiles: semi-colon separated list of files that will be watched for changes to restart the server 54 * node_env: will be propagated to node as NODE_ENV environment variable 55 * debuggingEnabled - controls whether the built-in debugger is enabled 56 57 See https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config for a full list of options 58 --> 59 <!--<iisnode watchedFiles="web.config;*.js"/>--> 60 </system.webServer> 61 62<!--ここに追加--> 63 <location path="sub.html"> 64 <system.webServer> 65 <authorization> 66 <allow users="*@gmail.com"/> 67 <deny users="*"/> 68 </authorization> 69 </system.webServer> 70 </location> 71 72</configuration>

エラー

これでサイトを表示させようとすると、以下のメッセージが表示されました。

The page cannot be displayed because an internal server error has occurred.

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問