Login failed for user '<token-identified principal>'

See original GitHub issue

Environment

  • Python: 64-bit
  • pyodbc: 4.0.30
  • OS: Windows
  • DB: SQL Server
  • driver: ODBC Driver 17 for SQL Server

Issue

Expected behavior

Establish a connection to SQL Server database.

Observed behavior

Getting an error that I don’t have permission, when I can connect and run basic queries in MSSMS. Only thing I can think of is that there needs to be some sort of permissions thing done on the backend, but it doesn’t make sense that it worked through SQL Server but not pyodbc.

Authentication: image

Basic Query: image

Code

import pyodbc

server = 'my_server.database.windows.net' 
database = 'my_db' 
username = '{me@gmail.com}'

cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username + '; AUTHENTICATION=ActiveDirectoryInteractive')

Error

InterfaceError: ('28000', "[28000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user '<token-identified principal>'. (18456) (SQLDriverConnect); [28000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user '<token-identified principal>'. (18456)")

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
v-chojascommented, Jul 23, 2020

Just to be sure, you are using the same credentials when you run the Python as you did in the successful DSN test above?

The way the AzureAD authentication to SQL works is that it authenticates to AAD first to get an access token, and then submits that token to SQL during login. What your symptoms show is that in both cases the authentication is successful, but in one case the server rejects the access token.

You could also try to remove the Database from the connection string in Python, and let Azure connect you to th

0reactions
stevenhurwittcommented, Jul 23, 2020

Thanks so much, that did it! Closing issue now.

EDIT: i’m an idiot - was using DATABASE=database.obj.table instead of DATABASE=database… wow*

Read more comments on GitHub >

github_iconTop Results From Across the Web

AAD Auth Error - Login failed for user '<token-identified ...
An error may appear when trying to login to Azure SQL DB using AAD authentication ... Login failed for user '<token-identified principal>'.
Read more >
Login Failed For User Token-Identified Principal In Azure SQL ...
Step 1 - · Step 2 – Set Azure AD admin in SQL Server · Step 3 – Sign in to SQL Database...
Read more >
Login failed for user '<token-identified principal>' when web ...
Login failed for user '<token-identified principal>' when web app is in an AAD Group · Deleting and recreating AAD Groups. · Scaling Web...
Read more >
"Login failed for user '<token-identified principal>" · Issue #1408
Describe the bug When trying to use service principal authentication, always getting the following error: Login failed for user ' Microsoft.
Read more >
Users created by PowerShell gets '<token-identified principal>'
RDM Office 365/Azure AD Users created by PowerShell connected by database user,When the users tries to logging they get: Login failed for user...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found