Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node-SQL with node.js and MS integrated Security

I have some sample code that is successfully connecting to SQL Server using Microsoft SQL Server user name and password. But I was wondering if there is a way to use integrated security with this script. Basically which means use the logged in user's credentials without supplying a password in the script.

var sql = require('mssql');

var config = {
    server: '127.0.0.1', 
    database: 'master',  
    user: 'xx',
    password: 'xxx',

    options : {
        trustedConnection : true
         }
}


var connection = new sql.Connection(config, function(err) {
    // ... error checks
    if(err) {
    return console.log("Could not connect to sql: ", err);
    }

    // Query

    var request = new sql.Request(connection);
    request.query('select * from dbo.spt_monitor (nolock)', function(err, recordset) {
        // ... error checks

        console.dir(recordset);
    });

    // Stored Procedure


});
like image 546
user3821263 Avatar asked Jul 16 '14 16:07

user3821263


1 Answers

Wish I could add this as a comment but don't have enough reputation yet... but what happens when you run this without providing a username/password in the config object?

Windows Authentication happens at the login level so there is no need to provide it at the application level.

Just browsed the documentation and looks like you cannot provide a raw connection string to connect, but to connect you want to build something that looks like this:

var connectionString= 'Server=MyServer;Database=MyDb;Trusted_Connection=Yes;'

The source code of the mssql module is here: https://github.com/patriksimek/node-mssql/blob/master/src/msnodesql.coffee... maybe you can fork and do a pull request that would provide an optional flag whether to use Windows Authentication or not, and that flag would remove the Uid={#{user}};Pwd={#{password}} (as it's unneeded for Windows Authentication) from the CONNECTION_STRING_PORT variable in the module's source code.

like image 147
skilbjo Avatar answered Nov 15 '22 20:11

skilbjo