简体   繁体   中英

pyodbc connection to MSSQL 2005 server via IIS7

I have IIS7 installed and configured on my laptop and with a python cgi interface. I can run the python script from within Eclipse and get the results I am looking for from the database. But when I run the script from the webpage I get an authentication error. It seems that the connection string is not passing the credentials to the SQL server. Any thoughts?

import pyodbc
import cgi
import cgitb; cgitb.enable()

cnxn = pyodbc.connect(driver='{SQL Server}', 
                  server='SERVER\INSTANCE', 
                  Trusted_Connection='yes', 
                  database='Test_Chad', 
                  uid='SERVER\username', 
                  pwd='password')

def getRow():
    cursor = cnxn.cursor()
    cursor.execute("select user_id, user_name from users")
    row = cursor.fetchone()
    print 'name:', row.user_id     
    print 'name:', row.user_name

getRow()

Exception:

<class 'pyodbc.Error'>: ('28000', "[28000] [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user 'NT AUTHORITY\\ANONYMOUS LOGON'.
(18456) (SQLDriverConnect); [28000] [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user 'NT AUTHORITY\\ANONYMOUS LOGON'. (18456)") 
args = ('28000', r"[28000] [Microsoft][ODBC SQL Server Driver][SQL ... for user 'NT AUTHORITY\ANONYMOUS LOGON'. (18456)") 
message = ''

Decide which form of authentication you want to use, database or Windows. You can't use both.

If you want to use Windows authentication, change your connection setup to the following:

cnxn = pyodbc.connect(driver='{SQL Server}', server='SERVER\INSTANCE',  
                      database='Test_Chad', trusted_connection='yes') 

Connecting a web application to a database via Windows authentication requires some other configuration. If your web application uses anonymous authentication in IIS, the database connection will be made using the identity of the application pool in which the web app runs. Here is a screenshot of where this identity is set:

IIS 应用程序池标识

If you want to use simple database authentication, provide the appropriate values for uid and pwd , and remove Trusted_Connection=yes :

cnxn = pyodbc.connect(driver='{SQL Server}', server='SERVER\INSTANCE',  
                      database='Test_Chad', uid='username', pwd='password')

I use below code with Identity: as shown as,but not work:

cnxn = pyodbc.connect(
        "Driver={SQL Server Native Client 11.0};Server=MyServerName;Database=MyDbName;Trusted_Connection=yes;")

在此处输入图片说明


Then I just change my connection to below code and work perfect with Identity= Localsystem

cnxn = pyodbc.connect(
        "Driver={SQL Server Native Client 11.0};Server=MyServerName;Database=MyDbName; uid=MyUsernameLoginDb; pwd=MyDbPasswordLoginToDB; ")

If you have you application pool security set correctly (ie the Application Pool user has login and access rights on SQl Server) then you need to configure Anonymous Access to use the Application Pool Identity instead of its default value (IUSR). Go to the Authentication section IIS 7 --> Anonymous Authentication --> Right-click to edit --> select the option to use the Application pool identity.

Simply create one MYSQL user and pass it to uid and pwd.

CREATE LOGIN MyUser WITH PASSWORD = 'pass@123'; CREATE USER MyUser FOR LOGIN MyUser;

It will work, Don't forget to provide the required schema access.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM