【问题标题】:Where is the key in new Azure App service?新的 Azure App 服务的关键在哪里?
【发布时间】:2016-04-10 10:08:36
【问题描述】:

在使用经典的 Azure 移动服务时,您曾经获得一个密钥以及您的移动服务应用程序的 URL。此密钥还用于探索后端站点上的 API 并用作密码。

使用新的 Azure App 服务,您只需使用如下 URL 即可实例化移动服务客户端

private static readonly MobileServiceClient MobileService = new MobileServiceClient("https://thecityapp.club");

没有可用于 Azure 移动服务的关键 *第二个参数。现在使用什么作为密码来探索 Web 上的 API?

【问题讨论】:

  • 更新:密钥被称为应用程序密钥,现在不可用。

标签: azure azure-mobile-services azure-app-service-envrmnt


【解决方案1】:

如果需要,您可以为 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 脚本更新为,

[sampleAPI.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"
        });
    }]
};

[sampleAPI.json]

{
  "get": {
    "access": "anonymous"
  },
  "post": {
    "access": "anonymous"
  },
  "put": {
    "access": "anonymous"
  },
  "patch": {
    "access": "anonymous"
  },
  "delete": {
    "access": "anonymous"
  }
}

不要忘记将权限更改为“匿名”

6。将您的 Table 脚本更新为,

[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/

【讨论】:

    【解决方案2】:

    太好了,

    对于应用程序服务/移动应用程序,应用程序密钥不再使用/不需要,这就是它不再在门户上可用的原因。您可以使用上述代码实例化客户端,并在没有该信息的情况下开始使用服务 API。

    关于身份验证,请参阅此文档:https://azure.microsoft.com/en-us/documentation/articles/app-service-mobile-windows-store-dotnet-get-started-users/

    我希望这会有所帮助。

    【讨论】:

    • 很令人沮丧的是这个密钥已经过时了至少四个月,显然,但是 HTML/JavaScript 客户端库的文档仍然说你需要一个 AppKey 来实例化 MobileServiceClient。像这样的东西让 Azure 的“简易表格”功能变得不那么容易了。 (azure.microsoft.com/en-us/documentation/articles/…)
    • @pettys,很抱歉文档有点混乱。上面的链接仍然提到应用程序密钥的原因是因为该文档涵盖移动服务,而不是移动应用程序(这是删除应用程序密钥支持的地方)。正在进行一些工作以更清楚地分离文档以避免混淆(我经常这样做)。
    • 感谢您抽出宝贵时间回复 - 我很感激。不幸的是,我仍然希望我没有选择为这个非常小的项目尝试 Azure Mobile [App|Services]。以为这将是一个快速、有趣的副项目,带有一个很酷的新工具,结果却变成了一个又一个的挫败感,首先是 azure 门户问题,试图将移动应用程序简单表配置为使用 azure 存储作为后端 - 不能让它工作。很抱歉咆哮 - 我确实希望它有所帮助,祝你一切顺利,我非常感谢你让社区参与堆栈溢出。
    猜你喜欢
    • 2014-08-24
    • 2019-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-29
    • 1970-01-01
    • 2021-03-31
    • 2020-03-21
    • 1970-01-01
    相关资源
    最近更新 更多