0

I am trying to connect to SQL Server via ActiveDirectoryPassword authentication mechanism and I am using 'Tedious' for that.

However whenever I run my project I run into the:

Security token could not be authenticated or authorized

Below is my code sample which I derived after referring to PAGE ON STACKOVERFLOW and Microsoft official page guide.

var Connection = require('tedious').Connection;  
const config = {
server: 'server-name.database.windows.net',    

options: {
    database: 'database-name',
    encrypt: true,
    port: 1433        
        },
authentication: {
    type: "azure-active-directory-password",
    options: {
        userName: "myusername",
        password: "mypassword",
                
        }
    }
}
var connection = new Connection(config);  
connection.on("connect", err => {
    if (err) {                       
      console.error(err.message);
    } else {
        console.log("Connected"); 
    }
  });

connection.connect();

What can be the possible reason for this error? Thanks.

abhishek
  • 29
  • 4
  • Does the target server have a `.database.windows.net` domain? Is the client computer's time relatively correct? Does the account have MFA disabled? – AlwaysLearning Mar 08 '22 at 10:15
  • @AlwaysLearning, Yes the target server name have ".database.windows.net" domain. It goes as: "server-name.database.windows.net". I am not sure about the MFA and also not able to interpret what you mean by client computer time?. – abhishek Mar 08 '22 at 10:29
  • @AlwaysLearning, can you please elaborate? I know what's MFA and I am assuming we meet to turn it on in azure portal? And what exactly you meant by Client computer time? – abhishek Mar 08 '22 at 10:38
  • Many token authentication schemes pass objects with around `exp` (expires), `iat` (issued at), `nbf` (not before) claims/properties. Strange things can happen when any of the computers involved have clock drift. – AlwaysLearning Mar 08 '22 at 10:48

1 Answers1

0

Installing tedious version 12.3.0 resolved my issue. The code is correct.

abhishek
  • 29
  • 4