tedious: Security token could not be authenticated or authorized error ( Node JS connectivity with MS SQL Server using Tedious)
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 error:
Security token could not be authenticated or authorized
Below is my code sample which I derived after referring to https://tediousjs.github.io/tedious/api-connection.html and https://docs.microsoft.com/en-us/sql/connect/node-js/step-3-proof-of-concept-connecting-to-sql-using-node-js?view=sql-server-ver15:
`
var Connection = require('tedious').Connection;
const config = {
server: 'server-name',
options: {
database: 'database-name.database.windows.net',
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?. I read on Tedious documentation that:
Best Practices Tedious supports an authentication type: azure-active-directory-password. This is the recommended method because Tedious will handle the process of retrieving the Azure token internally instead of requiring the user to manually retrieve the token as shown below.
Does this mean I need to do some setting at server side or something on the azure portal etc?
Thanks in advance.
About this issue
- Original URL
- State: open
- Created 5 years ago
- Comments: 15 (7 by maintainers)
@MichaelSun90 , I was using ‘14.3.0’ before. Tedious >= 13.2.0 now needs a client and tenant id for connection authentication “azure-active-directory-password”.
With this version, issue is resolved irrespective of I use tenantid or not.