0

First, I'm not a developer so I apologize if this is a redundant or unhelpful question.

In Microsoft SQL Server Management Studio, I am able to connect to a certain server using Active Directory Universal Authentication, but I can't figure out a way to do this using Python.

When I login with the application, after I click connect, an Azure login screen pops up, and I am able to login with my username: [email protected] and password.

However, when I try to connect using pyodbc with the following code, I get the error below:

import pyodbc

def main():
    serverName = "name.database.windows.net"
    dbName = "DatabaseName"

    connection = pyodbc.connect("DRIVER={SQL Server};SERVER=" + serverName + 
                 ";DATABASE=" + dbName + 
                 ";[email protected];PWD=mypassword")
    cursor = connection.cursor()
    data = cursor.execute("select * from sometable;")
    allData = data.fetchall()
    connection.close()
    for i in allData:
        print(i)

if __name__== "__main__":
    main()

pyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Cannot open server 'domain' requested by the login. Client with IP address 'xx.xx.xx.xx' is not allowed to access the server. To enable access, use the Windows Azure Management Portal or run sp_set_firewall_rule on the master database to create a firewall rule for this IP address or address range. It may take up to five minutes for this change to take effect. (40615) (SQLDriverConnect); [42000] [Microsoft][ODBC SQL Server Driver][SQL Server]

I know there have been posts about similar issues, but I am not sure this is the same problem. I have to use Active Directory Universal Authentication for this server. I have tried all of the SQL Server drivers that return from pyodbc.drivers() with no luck. I am using the same IP address to login through SQL Server Management Studio as when I am attempting it through the Python program. Is there a different module I can use to login with Azure, or is this impossible to do?

For reference, the popups that I see when I login through Microsoft SQL Server Management Studio are below. I have been stuck on this for a while, so any help is much appreciated. enter image description here

enter image description here

1 Answer 1

1

I get the same error message if I have not set Azure SQL firewall: enter image description here

We can via azure portal to set SQL server firewall, like this: enter image description here

After I add the firewall settings, I can use your python script to query, python script works for me.

Sign up to request clarification or add additional context in comments.

7 Comments

I am having trouble understanding why I need to use a different IP address to login using Python than the one that I use to login in SQL Server Management Studio. Is this a configuration issue? I do not currently have the administrator permissions to change the firewall settings.
Does your python And ssms run on the same machine?
I am running them both from the same computer -- is it possible that SSMS switches over to run the login process on a virtual machine when I use this authentication method or could something weird with multifactor authentication be happening (sometimes I have to enter my password twice in two different popups in SSMS)?
@user5932842 yes, could you please change another authentication?
@user5932842 Have you try it? does that work for you?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.