如果需要,您可以为 Azure 移动应用程序实施应用程序密钥。
您可以为 Azure 移动应用程序(如 Azure 移动服务)设置应用程序密钥。
1. 在 Azure 移动应用程序上打开应用程序设置
2. 向下滚动到App Settings添加这两行。
| zumo-api-key | 输入您的 API 密钥|
| MS_SkipVersionCheck | 真 |
3.然后点击保存
4.打开应用服务编辑器
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. 将您的表脚本更新为,
[sampleTable.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;
[sampleTable.json]
{
"softDelete" : true,
"autoIncrement": false,
"insert": {
"access": "anonymous"
},
"update": {
"access": "anonymous"
},
"delete": {
"access": "anonymous"
},
"read": {
"access": "anonymous"
},
"undelete": {
"access": "anonymous"
}
}
不要忘记将权限更改为“匿名”
7. 完成!
调用 Azure 移动/Web 应用程序时不要忘记添加标头。
此外,您可以从 Github 上的这个存储库中看到更多信息。
https://github.com/thisisfatih/applicationKeyAzure/