Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to SQL Server with windows authentication from Node.JS using mssql module

Hi I'm unable to connect to SQL server that is using windows authentication in node js. I'm using the mssql module. The error message is :

[ConnectionError: Login failed for user ''. The user is not associated with a trusted SQL Server connection.]
name: 'ConnectionError',
message: 'Login failed for user \'\'. The user is not associated with a trusted SQL Server connection.',
code: 'ELOGIN' }

Here is my code:

config = {
    server : "localhost\\MSSQLSERVER",
    database : "mydatabase",
    port : 1433
}

function loadDepts() {
    var conn = new sql.Connection(config);
    var request = sql.Request(conn);

    conn.connect(function(err) {
    if (err) {
        console.log(err);
        return;
    }

    request.query("select deptid, deptname from departments", function(err, table) {
        if (err) {
           console.log(err);
           return;
        }
        else {
           console.log(table);
        }

        conn.close();
        });
    });
}

loadDepts();
like image 929
F0r3v3r-A-N00b Avatar asked Nov 14 '15 15:11

F0r3v3r-A-N00b


People also ask

How do I connect to Windows authentication in SQL Server node JS?

Run the Node. Install all required npm packages by running npm install or npm i from the command line in the project root folder (where the package. json is located). Update the database credentials in /config. json to connect to your MS SQL Server instance, and ensure MSSQL server is running.

How do I connect to mssql Windows authentication?

Open SQL Server Management Studio. In Connect to Server, select Database Engine, enter your SQL Server name, and enter administrator credentials to connect to the server. Select Connect. In Object Explorer, expand the SQL Server, expand Security, right-click Logins, and then select New Login.

How do I connect to SQL Server with SQL authentication?

Right-click the server you wish to modify and then click Properties. Select the Security Page. Under the Server authentication heading choose either the desired authentication: Windows Authentication or SQL Server and Windows Authentication mode. Click OK.

How do I connect to SQL Server using Windows authentication in PowerShell?

By default, the SQL Server PowerShell components use Windows Authentication when connecting to an instance of the Database Engine. You can use SQL Server Authentication by either defining a PowerShell virtual drive, or by specifying the -Username and -Password parameters for Invoke-Sqlcmd.


2 Answers

Since this is a fairly visible answer, I wanted to add in a code snippet that worked for me with Trusted Connection. Got to it from getglad's edited answer.

const sql = require("mssql");
require("msnodesqlv8");
const conn = new sql.Connection({
  database: "db_name",
  server: "server_name",
  driver: "msnodesqlv8",
  options: {
    trustedConnection: true
  }
});
conn.connect().then(() => {
  // ... sproc call, error catching, etc
  // example: https://github.com/patriksimek/node-mssql#request
});

Using trusted connection, I was able to execute stored procedures, log the output, and close the connection without any trouble, and msnodesqlv8 has been updated more recently than any of the other drivers (latest release was October 2016 as of 11/3/2016), so that seems to be a safe choice as well.

And here's an example using [email protected]. The only changes are the initial require, which pull in msnodesqlv8 from within mssql, and sql.Connection is now sql.ConnectionPool. You will also need to change your stored procedure calls since the response is different, noted here. Credit to Jon's answer since he updated mine before I did!

const sql = require("mssql/msnodesqlv8");
const conn = new sql.ConnectionPool({
  database: "db_name",
  server: "server_name",
  driver: "msnodesqlv8",
  options: {
    trustedConnection: true
  }
});
conn.connect().then(() => {
  // ... sproc call, error catching, etc
  // example: https://github.com/patriksimek/node-mssql#request
});
like image 146
Aaron Ballard Avatar answered Oct 23 '22 21:10

Aaron Ballard


I have been struggling too for some time about how to use mssql + Windows Auth, here is how i got it to work on my project.

As pointed out in the mssql documentation, you need msnodesqlv8 installed too.

npm install msnodesqlv8

Now, following on Aaron Ballard's answer, you use it like this:

const sql = require('mssql/msnodesqlv8')

const pool = new sql.ConnectionPool({
  database: 'database',
  server: 'server',
  driver: 'msnodesqlv8',
  options: {
    trustedConnection: true
  }
})

pool.connect().then(() => {
  //simple query
  pool.request().query('select 1 as number', (err, result) => {
        console.dir(result)
    })
})

As a note, i tried to add this as a comment on Aaron's answer, as mine is just a complement/update to his, but i don't have enough reputation to do so.

like image 28
Jon Koala Avatar answered Oct 23 '22 20:10

Jon Koala