Adding username/password to Connection

4701
7
04-06-2016 07:32 AM
PaulYoum1
New Contributor II

I was wondering if there was a way of using a pre-existing connection that does not have a username/password set to it by default. Then, in my python script, I could prompt the user for username and password, and then pass the credentials to the connection string, as opposed to having the user wait for the prompt.

My main issue with the prompt is that it doesn't work with the script when it's "run in foreground" because the user can't interact with the username/password dialog... since it's in the background... (seems like a bug to me!)

But if I don't have the script running in the foreground, I don't get the dialog box to present to the user the progress bar.

So far, the only solution that might seem viable is to create a new connection file, use that connection file, and then delete the connection file when the python script is complete. Found it in this thread here:Cannot Acces SDE Connection File Properly using Arcpy

Is there any way for me to inject a username/password into the pre-existing connection?

Tags (2)
0 Kudos
7 Replies
DanPatterson_Retired
MVP Esteemed Contributor

Did you run through the Script example in Running custom (model or script) tools in the background—Help | ArcGIS for Desktop to see if you can set up your script to ensure foreground running.  Sounds like you want to do the reverse of running in background.

PaulYoum1
New Contributor II

Hi Dan! Yes I've ensured the script is running in the foreground and did do those steps. The problem with it running in the foreground is that you can see in the image I provided that the dialog box in the background is unfocusable because the script is "run in foreground." So the user can't supply their credentials because the script is in the foreground.

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

I have no need for checks like that, however, if in a pinch I use python's builtin 'input' should I need human intervention into a standalone script rather than editing the script

n = input('input password')

but I have no idea if this would behave well with script tools.

0 Kudos
PaulYoum1
New Contributor II

Hi Dan,

So I have a way to prompt the user for a username/password. My issue is the connection. Ideally, I would like to supplant the username/password into my connection string somehow so that I can pass the user credentials that way. I realize that perhaps ESRI might not like this as the password would need to be encrypted so it won't be passed in the clear, but I don't know how their connection strings are formatted. I couldn't find documentation to go through the "ESRI" way. I know I can create my own database connection string, but I'm trying to interface as much as possible utilizing the ESRI abstraction layer.

So really, I'm looking to supply the username password I get from the prompt to the user, and embed that into the pre-existing database connection.

0 Kudos
AndrewKeith3
Occasional Contributor

Can you use the Create Database Connection tool in python with the supplied username/password as a parameter in the Script tool (String Hidden type) and then delete sde file when script completes.  You could put the sde file in the arcpy.env.scratchFolder environment setting. arcpy.CreateDatabaseConnection_management

PaulYoum1
New Contributor II

Yeah this is the route I'm going, but I was hoping to avoid this route. But thanks for the tip on the arcpy.env.scratchFolder!

0 Kudos
JuliaLenhardt
Esri Contributor

Hey Paul - my main question to you is, why do you want to prompt the user for a username and password in the middle of the script? In the GP framework, stopping a script part-way through to prompt for a parameter isn't a supported workflow, which is part of the reason why the window doesn't come up in front of the script dialog while the script is running. Also, this puts a lot of the weight on you for making sure the other connection information is all correct (what if the server name changes, or a new version in the geodatabase is going to be used, or a new database is created?).

Instead, why not create a script tool in a toolbox that requires an SDE connection file in order to run? This way, the user is responsible for creating their own SDE connection file in ArcGIS. Then you can create a script tool with an SDE connection file as an input parameter, so that when the user goes to run the script tool they are prompted to point to their connection file. This way you don't have to create and delete a connection file every time you run the script. If these users are utilizing a connection file for any reason, they will have saved their connection files in the Database Connections file for regular use.