401 - 未经授权的访问:Auth0和azure

时间:2017-07-17 00:11:39

标签: javascript azure authentication auth0 azure-sql-database

我已将数据库连接到auth0,当我尝试连接时返回401未经授权的访问。我已经允许auth进入我的防火墙,密码是正确的。为什么在搜索用户名和密码时返回此错误?

更多信息

在我简单的表格中,我只对其进行了身份验证访问,我是否需要做些什么来解决这个问题?

function login(email, password, callback) {
      //this example uses the "tedious" library
      //more info here: http://pekim.github.io/tedious/index.html
      var Connection = require('tedious@1.11.0').Connection;
      var Request = require('tedious@1.11.0').Request;
      var TYPES = require('tedious@1.11.0').TYPES;

      var connection = new Connection({
        userName: 'username',
        password: 'pass',
        server: 'server',
        options: {
          database: 'db',
          encrypt: true,
          rowCollectionOnRequestCompletion: true
        }
      });

      var query = "SELECT Id, Email, Password " +
        "FROM user WHERE Email = @Email";

      connection.on('debug', function (text) {
        // Uncomment next line in order to enable debugging messages
        // console.log(text);
      }).on('errorMessage', function (text) {
        console.log(JSON.stringify(text, null, 2));
        return callback(text);
      }).on('infoMessage', function (text) {
        // Uncomment next line in order to enable information messages
        // console.log(JSON.stringify(text, null, 2));
      });

      connection.on('connect', function (err) {
        if (err) { return callback(err); }

        var request = new Request(query, function (err, rowCount, rows) {
          if (err) {
            callback(new Error(err));
          } else if (rowCount < 1) {
            callback(new WrongUsernameOrPasswordError(email));
          } else {
            bcrypt.compare(password, rows[0][2].value, function (err, isValid) {
              if (err) { callback(new Error(err)); }
              else if (!isValid) { callback(new WrongUsernameOrPasswordError(email)); }
              else {
                callback(null, {
                  user_id: rows[0][0].value,
                  email: rows[0][1].value
                });
              }
            });
          }
        });

        request.addParameter('Email', TYPES.VarChar, email);
        connection.execSql(request);
      });
    }

1 个答案:

答案 0 :(得分:0)

由于您使用的是Azure移动应用,其中包含the Node.js server SDK适用于您的应用。然后,您无需安装tedious即可使用Azure SQL数据库。 SDK已经包含mssql来执行此操作。所以基本上你可以使用this code sample来连接你的数据库。

var api = {
    // an example of executing a SQL statement directly
    get: (request, response, next) => {
        var query = {
            sql: 'UPDATE TodoItem SET complete = @completed',
            parameters: [
                { name: 'completed', value: request.query.completed }
            ]
        };

        request.azureMobile.data.execute(query)
            .then(function (results) {
                response.json(results);
            });
    },
    // an example of executing a stored procedure
    post: (request, response, next) => {
        var query = {
            sql: 'EXEC completeAllStoredProcedure @completed',
            parameters: [
                { name: 'completed', value: request.query.completed }
            ]
        };

        request.azureMobile.data.execute(query)
            .then(function (results) {
                response.json(results);
            });
    }
};

module.exports = api;

有关详细信息,请参阅this documentation