0

I have a flask application that connects to a database on SQL Server which I am running on windows. I am using an Active Directory Service account for establishing the connection and this is how my connection string is -

# connect_db function is used to connect with the database for read and write purposes
def connect_db():
    connection = pyodbc.connect(r"Driver={SQL Server Native Client 11.0};Server=server,port;Database=db;UID=domain\username;PWD=password;", autocommit=True)
    return connection

Here is the error message -

pyodbc.InterfaceError: ('28000', "[28000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user 'domain\\username'. (18456) (SQLDriverConnect); [28000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user 'domain\\username'. (18456)")

Looks like the string is not escaping the \ in the UID I have tried \\ and also used r' as a raw string at the beginning of the string but it does not work.

I cannot use runas as I would like the web application to be deployed on a server in the future. When I use the same account on SSMS I am able to login successfully.

heisenbug29
  • 123
  • 1
  • 10
  • Have a look at Gord Thompson's [answer](https://stackoverflow.com/a/37702329/5400385) – PGHE Jul 28 '22 at 19:45
  • @PGHE I cannot use `Run As`, how would I deploy the app on IIS then? – heisenbug29 Jul 28 '22 at 20:10
  • @PGHE I also don't want to use `Trusted_Connection=Yes` as it logs in with my own user account and not the active directory service account – heisenbug29 Jul 28 '22 at 20:15
  • You cannot specify a Windows/Active Directory username and password like that. Whenever you specify `UID` and `PWD` connection parameters that means to use _SQL Login_ authentication. It seems like you should be using `Trusted_Connection=Yes` to enable Windows/Active Directory authentication and then, in IIS, configure the Application Pool in which the Python page(s) reside to use the Active Directory Service account credentials and turn off Impersonation so that it's not trying to use the Windows/Active Directory authentication of the connecting browser user(s). – AlwaysLearning Jul 29 '22 at 04:04
  • @AlwaysLearning is there another driver or set up that I could use to input Active Directory UID and PWD ? – heisenbug29 Aug 01 '22 at 15:09

0 Answers0