1

I am able to connect to a SQL Server database using a SQL account. How do I connect to the same SQL Server using windows auth? I have already added trustedConnection as true.

Note - I am passing the password for the domain\testetl account because I am running this from AWS Lambda where I cannot login using that user.

'use strict';


const sql = require('mssql');

exports.handler = (event, context, callback) => {
  const config = {
    server: 'ip',
    port: 45,
    user: 'domain\testetl',
    password: 'g',
    database: 'central',
    options: {
    trustedConnection: true
  }
  };

  sql.connect(config, (err) => {
    if (err) {
      console.log(err);
      callback(err);
    } else {
      const req = new sql.Request();
      req.query('SELECT top 10 * FROM STATS', (error, result) => {
        if (error) {
          console.log(error);
          callback(error);
        } else {
          console.log(result);
          sql.close();
          callback(null, result.recordset);
        }
      });
    }
  });

  sql.on('error', (err) => {
    console.log(err);
    callback(err);
  });
};
Aaron
  • 1,345
  • 2
  • 13
  • 32
  • Possible duplicate of [How to connect to SQL Server with windows authentication from Node.JS using mssql module](https://stackoverflow.com/questions/33709807/how-to-connect-to-sql-server-with-windows-authentication-from-node-js-using-mssq)? – Thom A Mar 13 '19 at 16:52
  • Possible duplicate of [How to connect to SQL Server with windows authentication from Node.JS using mssql module](https://stackoverflow.com/questions/33709807/how-to-connect-to-sql-server-with-windows-authentication-from-node-js-using-mssq) – alroc Mar 13 '19 at 16:53
  • Seems we were thinking the same @alroc. I don't know enough about Node.JS, so I'm not going to vote on this; as it'll automatically lock the question. – Thom A Mar 13 '19 at 16:54
  • This is not a duplicate because I have already added `trustedConnection` as mentioned in the other question and still get a `Login Failed` error message. @Larnu @alroc – Aaron Mar 13 '19 at 16:54
  • But, @Aaron ,either of those example has the password in the connection string. Something that is common when using Windows Authentication; as the user is *already* authenticated. – Thom A Mar 13 '19 at 16:55
  • @Larnu As I mentioned in my question, I have to pass the password because I am running it from AWS where Windows Auth wont work. – Aaron Mar 13 '19 at 16:56
  • That isn't how trusted connections work though @Aaron . You're trying to mix 2 different authentication methods. I suspect those links do cover what you need. But if you can't authenticate to the AD Controller from the host trying to connect to SQL Server, then you won't be able to use a trusted connection. If you have an AWS Server which can't authenticate to your domain, then a trusted connection is off the cards. – Thom A Mar 13 '19 at 17:00
  • I have added the `aws-lambda` tag to see if anyone else has any ideas. @Larnu – Aaron Mar 13 '19 at 17:10

0 Answers0