0

I am trying to connect to SQL Server in our domain network. I am able to connect using python but not able to connect in Node.js using Tedious.

Node.js code snippet:

     var config = {
               server: 'serverName.domain.com',
               authentication: {
                       type: 'default',
                       options: {
                         userName: 'DOMAINID\\username',
                         password: 'password'
                                 }
                               },
                options: {
                      database: 'dbName',
                      port: 1234,
                          }
              };

var connection = new Connection(config);

  connection.on('connect', function (err) {
    if (err) {
      console.log('err', err);
    } else {
     console.log("Connected");
     executeStatement();
   } 
 }); 
connection.connect();

Receiving error:

Login Failed for the user DOMAINID/username. The login is from an untrusted domain and cannot be used with Windows authentication.

But when trying to connect from Python, I am able to connect successfully.

Python snippet:

import sqlalchemy

conn = sqlalchemy.create_engine('mssql+pymssql://DOMAINID\\username:[email protected]:1234/dbName')
print(conn.execute('SELECT * FROM table_name').fetchall())

Data received successfully in python.

And also I tried with mssql and msnodesqlv8 with Microsoft ODBC 11 for Microsoft SQL Server drivers.

I am able to connect. Following is the code snippet.

const sql = require("mssql/msnodesqlv8");

const main = async () => {
const pool = new sql.ConnectionPool({
    server: "server.domain.com",
    database: "dbName",
    port: 1234,
    user:'DomainId\\username',  // Working without username and password 
    password:'password',
    options: {
        trustedConnection: true // working only with true
    }
});

await pool.connect();

const request = new sql.Request(pool);

const query = 'select * from table';

const result = await request.query(query);

console.dir(result);
};
main();

In the above snippet, I am able to connect without username and password but with trustedConnection true only. I am using windows authentication not SQL authentication. How can I connect using tedious js

8
  • Windows authentication doesn't use a username and password. That's the whole point - the code connects using the account that executes the program. I'm surprised the Python string works at all. Commented Mar 4, 2022 at 9:04
  • The error is pretty clear too - are you trying to connect with a non-domain account? As the error says, that doesn't work. Use a domain account instead. If your machine isn't joined to a domain, make sure it is Commented Mar 4, 2022 at 9:08
  • is there any option to specify tedious to not to use windows credentials and take credentials we specified Commented Mar 4, 2022 at 9:43
  • You haven't specified any valid credentials at all. You can either use Windows or SQL authentication. You can't pass Windows credentials in the connection string as if they were SQL account credentials. The whole point of Windows authentication is that no explicit credentials are needed, so there's no need to store (and leak) passwords. Commented Mar 4, 2022 at 9:46
  • 1
    You aren't using SQL credentials. A SQL account would be named potato not SomeDomain\potato. SQL accounts have no domains. A SQL account can't have backslashes Commented Mar 4, 2022 at 10:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.