【问题标题】:Azure mobile services scriptsAzure 移动服务脚本
【发布时间】:2016-07-20 20:31:29
【问题描述】:

我对使用服务器脚本非常陌生,我确信这是一个非常简单的答案,但我还没有在任何地方找到我想要的东西。我正在使用 azure 移动服务来检索和输入用户信息,并希望防止用户看到其他用户的 ID 号。

为了读取数据,程序发出以下http请求

"GET http://Servicename.net/tables/TableName?$top=1&$orderby=__createdAt%20desc&$filter=id+eq+'"+id+"' HTTP/1.1"

id 由用户当前使用的帐户确定,但我不希望用户能够删除 id,发出如下请求并检索整个表以及其他用户 id

 "GET http://Servicename.net/tables/TableName HTTP/1.1"

我想做的是使用服务器端脚本,特别是读取操作脚本,以确保请求具有与之关联的 id,并且只返回具有该特定 id 的数据。

我尝试了以下方法:

function read(query, user, request) {
 if(request.parameter.id != null){
request.execute();
    }
}

这不起作用,所以我的问题是如何从 http 请求中检索 id 号并在脚本中使用它?我希望我的问题很清楚,非常感谢任何帮助!

【问题讨论】:

    标签: javascript azure azure-mobile-services


    【解决方案1】:

    我会向您推荐我的 Azure 移动应用系列:https://shellmonger.com/30-days-of-azure-mobile-apps-the-table-of-contents/

    尤其是第 6 天 - 个人餐桌。这显示了如何限制通过身份验证的用户 ID 返回给用户的数据。

    【讨论】:

      【解决方案2】:

      如果要在Mobile Service的表操作中检索用户ID。您可以使用第二个参数user,它始终是代表提交请求的用户的user object。

      你可以在https://azure.microsoft.com/en-us/documentation/articles/mobile-services-how-to-use-server-scripts/#table-scripts找到一个代码sn-p来解释用法:

      function insert(item, user, request) {
          if (item.userId !== user.userId) {
              request.respond(statusCodes.FORBIDDEN, 
              'You may only insert records with your userId.');
          } else {
              request.execute();
          }
      }
      

      有关 Node.js 中的移动服务的更多信息,请参阅 Work with a JavaScript backend mobile service。

      此外,现在我们建议使用移动应用程序而不是移动服务。详情可以参考https://azure.microsoft.com/en-us/documentation/articles/app-service-mobile-value-prop-migration-from-mobile-services/。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-09-04
        • 1970-01-01
        • 2016-04-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-19
        相关资源
        最近更新 更多