必要に応じて、Azure モバイル アプリのアプリケーション キーを実装できます。
Azure モバイル サービスのような Azure モバイル アプリのアプリケーション キーを設定できます。
1. Azure モバイル アプリケーションでアプリケーション設定を開きます
2.アプリの設定まで下にスクロールし、次の 2 行を追加します。
| | zumo-api-key | API キーを入力|
| | MS_SkipVersionCheck | 真 |
3. 次に、[保存] をクリックします。
4.App Service エディターを開く
5. メイン フォルダーwwwrootにファイルを作成します。
6. ファイルにvalidateApiKey.jsという名前を付けます
// ----------------------------------------------------------------------------
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
// ----------------------------------------------------------------------------
module.exports = function (req, res, next) {
// Validate zumo-api-key header against environment variable.
// The header could also be validated against config setting, etc
var apiKey = process.env['zumo-api-key'];
if (apiKey && req.get('zumo-api-key') != apiKey)
return res.status(401).send('This operation requires a valid api key');
else
return next();
}
6. API スクリプトを次のように更新します。
【サンプルAPI.js】
var validateApiKey = require('../validateApiKey');
module.exports = {
"get": [validateApiKey, function(request, response, next)
{
response.send(
{
message: "post"
});
}],
"post": [validateApiKey, function(request, response, next)
{
response.send(
{
message: "post"
});
}]
};
[サンプルAPI.json]
{
"get": {
"access": "anonymous"
},
"post": {
"access": "anonymous"
},
"put": {
"access": "anonymous"
},
"patch": {
"access": "anonymous"
},
"delete": {
"access": "anonymous"
}
}
権限を「匿名」に変更することを忘れないでください
6. テーブル スクリプトを次のように更新します。
[サンプルテーブル.js]
var azureMobileApps = require('azure-mobile-apps'),
validateApiKey = require('../validateApiKey');
// Create a new table definition
var table = azureMobileApps.table();
// Access should be anonymous so that unauthenticated users are not rejected
// before our custom validateApiKey middleware runs.
table.access = 'anonymous';
// validate api key header prior to execution of any table operation
table.use(validateApiKey, table.execute);
// to require api key authentication for only one operation (in this case insert)
// instead of table.use(validateApiKey, table.execute) use:
// table.insert.use(validateApiKey, table.operation);
module.exports = table;
[サンプルテーブル.json]
{
"softDelete" : true,
"autoIncrement": false,
"insert": {
"access": "anonymous"
},
"update": {
"access": "anonymous"
},
"delete": {
"access": "anonymous"
},
"read": {
"access": "anonymous"
},
"undelete": {
"access": "anonymous"
}
}
権限を「匿名」に変更することを忘れないでください
7. 完成!
Azure Mobile/Web アプリを呼び出すときにヘッダーを追加することを忘れないでください。
また、Github のこのリポジトリからさらに見ることができます。
https://github.com/thisisfatih/applicationKeyAzure/