tediousjs / tedious

Node TDS module for connecting to SQL Server databases.

Home Page:http://tediousjs.github.io/tedious/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unable to connect to SQL Server using access token using

pdmateus opened this issue · comments

I need to access Azure SQL from node.js (Azure Function) using access token "azure-active-directory-access-token" or 'azure-active-directory-service-principal-secret' but its failing with:

Login failed for user '

I'm using mssql-node and tedious.

This is my config:
var config = {
server: 'serverA.database.windows.net',
authentication: {
type: 'azure-active-directory-access-token',
options: {
token: 'token_here'
}
},
"options": {
debug: {
packet: true,
data: true,
payload: true,
token: false,
log: true
},
database: 'db_name',
encrypt: true
}
};

I've tested many different iterations of this and even msnodesqlv8 (which doesn't support access token right?)

Using c# (Az Function) it works seamlessly, exact same token:

        using (SqlConnection conn = new SqlConnection("Data Source = serverA.database.windows.net; Initial Catalog =db_name; Connect Timeout = 30"))
        {
            conn.AccessToken = "token_here";
            SqlCommand cmd = new SqlCommand("select @@version", conn);

I'm using PS initially to get the token and test it out:
$ClientCred = [Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential]::new($spnid, $secret)
$authContext = [Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext]::new($authority)
$authResult = $authContext.AcquireTokenAsync($resourceAppIdURI, $ClientCred)

I've also been testing trying to get the token within app but failing, unsure why but it should work with token directly right?

I'm new to node.js so I might be missing something very simple, any help is appreciated thank!

Hi @pdmateus, just searched around and found this #1225. I am not sure which version of mssql and tedious you are on, but from that closed ticket, seem that some earlier version of mssql does not work with Azure token. You could try to get a later version of mssql and tedious and see if that works. Hope this helps.

Hi thanks @MichaelSun90, I have the latest versions on both I believe...I just installed it with npm

"mssql": "^7.2.0",
"tedious": "^12.0.0"

I'm unsure why, it could be that mssql was using internally a tedious old version, but when I switched fully to only using tedious and stopped using mssql, I got it to work.