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:password@serverName.domain.com: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

Sudheer
  • 1
  • 4
  • 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. – Panagiotis Kanavos Mar 04 '22 at 09: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 – Panagiotis Kanavos Mar 04 '22 at 09:08
  • is there any option to specify tedious to not to use windows credentials and take credentials we specified – Sudheer Mar 04 '22 at 09: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. – Panagiotis Kanavos Mar 04 '22 at 09:46
  • Why isn't the client machine on the same domain as the server? If you want to use Windows auth, it *has* be be on the same domain. Otherwise you'll have to create and use SQL accounts. – Panagiotis Kanavos Mar 04 '22 at 09:49
  • client machine is on the same domain as the server. I am trying to connect using SQL credentials not windows credentials – Sudheer Mar 04 '22 at 09:59
  • 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*](https://learn.microsoft.com/en-us/sql/t-sql/statements/create-login-transact-sql?view=sql-server-ver15#login_name) – Panagiotis Kanavos Mar 04 '22 at 10:00
  • Yes @PanagiotisKanavos you are right i am not using SQL authentication. I am using Windows Authentication. How can I connect using tedious js – Sudheer Mar 17 '22 at 06:10

0 Answers0