<jupyter_start><jupyter_text>Working with different authentication schemesThe `GIS` class in the ArcGIS API for Python supports several classes of users with varied authentication schemes:* [Anonymous users](Anonymous-users)* [Built-in users](Built-in-users)* [Enterprise identity store](Enterprise-identity-store)  * [Web-tier authentication with LDAP](Web-tier-authentication-with-LDAP)  * [Portal-tier authentication with Active Directory](Portal-tier-authentication-with-Active-Directory)  * [Portal-tier authentication with LDAP](Portal-tier-authentication-with-LDAP)  * [Web-tier authentication secured with PKI](Web-tier-authentication-secured-with-PKI)  * [Web-tier authentication secured with IWA](Web-tier-authentication-secured-with-IWA)* [User authentication with OAuth 2.0](User-authentication-with-OAuth-2.0)* [Connecting through ArcGIS Pro](Connecting-through-ArcGIS-Pro)* [Protecting your credentials](Protecting-your-credentials)* [Storing your credentials locally](Storing-your-credentials-locally)The `GIS` class supports constructing a `GIS` object given an url and user credentials to ArcGIS Online or an ArcGIS Enterprise instance. User credentials can be passed in using username / password pair, or using a key_file / cert_file pair (in case of PKI). The `GIS` class also supports built-in users, LDAP, PKI and anonymous access.If no url is provided, ArcGIS Online is used. If username / password or key / cert files are not provided, anonymous access is used. Additionally, the script can leverage the ArcGIS Pro app to login to the portal and their scripts can use whichever Portal is currently active.The examples below demonstrate how to connect using these different authentication schemes:<jupyter_code># import the GIS class in gis module
from arcgis.gis import GIS<jupyter_output><empty_output><jupyter_text>Anonymous usersYou can make use of the Python API as anonymous user. However, tasks that you can accomplish are fewer, as you would not be able to create or modify content or perform any analysis tasks. As an anonymous user, you can query and view publicly shared maps and layers. Connect to ArcGIS Online as an anonymous user<jupyter_code>print("ArcGIS Online as anonymous user")    
gis = GIS()
print("Logged in as anonymous user to " + gis.properties.portalName)<jupyter_output>ArcGIS Online as anonymous user
Logged in as anonymous user to ArcGIS Online<jupyter_text>Connect to your ArcGIS Enterprise as an anonymous userIf your ArcGIS Enterprise allows anonymous usage, you can connect to it as such by specifying the URL and leaving the username and password parameters empty. Note: If your enterprise is configured with IWA authentication, and if you are running this on a Windows computer, you might get automatically signed in.<jupyter_code>gis = GIS("https://pythonapi.playground.esri.com/portal")
print("Logged in as anonymous user to " + gis.properties.portalName)<jupyter_output>Logged in as anonymous user to Portal for ArcGIS<jupyter_text>Built-in usersBoth ArcGIS Online and ArcGIS Enterprise come preconfigured with a [built-in identity store](http://server.arcgis.com/en/portal/latest/administer/windows/about-configuring-portal-authentication.htmESRI_SECTION1_83F7B85FEF594A6B96997AF3CADF3D38), so you can easily create accounts and groups in your portal. When signing in to such accounts, specify your username and password, as you type them into the portal website. Connect to ArcGIS Online using a built-in account<jupyter_code>print("ArcGIS Online Org account")    
gis = GIS("https://www.arcgis.com", "arcgis_python", "<PASSWORD>")
print("Logged in as " + str(gis.properties.user.username))<jupyter_output>ArcGIS Online Org account
Logged in as arcgis_python<jupyter_text>Connect to ArcGIS Enterprise using a built-in account<jupyter_code>print("Portal for ArcGIS as a built in user")
gis = GIS("https://portalname.domain.com/webadapter_name", "sharinguser", "password")
print("Logged in as: " + gis.properties.user.username)<jupyter_output>Portal for ArcGIS as a built in users
Logged in as sharinguser<jupyter_text>Enterprise identity storeArcGIS Online and Enterprise is designed so you can use your enterprise accounts and groups to control access to your ArcGIS organization. Web-tier authentication with LDAP<jupyter_code>print("\n\nBasic Authentication with LDAP")    
ldapbasic = GIS("https://portalname.domain.com/webadapter_name", "amy", "password")
print("Logged in as: " + ldapbasic.properties.user.username)<jupyter_output>Basic Authentication with LDAP
Logged in as: amy<jupyter_text>Portal-tier authentication with Active DirectoryWhen connecting using an enterprise user account from an Active Directory, specify your username as `domain\\username`<jupyter_code>print("\n\nPortal-tier Authentication with LDAP - enterprise user")
gisldap = GIS("https://portalname.domain.com/webadapter_name", "AVWORLD\\Publisher", "password")
print("Logged in as: " + gisldap.properties.user.username)<jupyter_output>Portal-tier Authentication with LDAP - enterprise user
Logged in as: Publisher@AVWORLD<jupyter_text>Portal-tier authentication with LDAPWhen connecting using an enterprise user account from LDAP, specify your username and password as usual<jupyter_code>print("\n\nPortal-tier Authentication with LDAP - builtin user")    
gisldap = GIS("https://portalname.domain.com/webadapter_name", "sharing1", "password")
print("Logged in as: " + gisldap.properties.user.username)<jupyter_output>Portal-tier Authentication with LDAP - builtin user
Logged in as: sharing1<jupyter_text>Web-tier authentication secured with PKIYou can also use two different methods of connecting to a PKI protected ArcGIS Enterprise.First, you can specify a local PEM-encoded certificate and a 'key' file to use as client side certificate, when logging into your ArcGIS Enterprise using PKI-based client certificate authentication.<jupyter_code>print("\n\nPKI with key and cert files")  
gis = GIS("https://portalname.domain.com/webcontext", 
          key_file="C:\\path\\to\\key.pem",
          cert_file="C:\\path\\to\\cert.pem")
print("Logged in as: " + gis.properties.user.username)<jupyter_output>PKI with key and cert files
Logged in as: username@DOMAIN<jupyter_text>Second, you can use a PKCS12 formatted certificate file (for example .pfx or .p12) and password when logging into your ArcGIS Enterprise using PKI-based client certificate authentication.<jupyter_code>print("\n\nPKI with PFX file and password")  
gis = GIS("https://portalname.domain.com/webcontext", 
          cert_file="C:\\path\\to\\mycert.pfx",
          password="<PASSWORD>")
print("Logged in as: " + gis.properties.user.username)<jupyter_output>PKI with PFX file and password
Logged in as: <EMAIL><jupyter_text>Web-tier authentication secured with IWAIf your Enterprise is configured with the [ArcGIS Portal WebAdaptor for IIS on Microsoft's IIS Web Server and enabled for Windows Authentication with Kerberos or NTLM providers](https://enterprise.arcgis.com/en/web-adaptor/latest/install/iis/use-integrated-windows-authentication-with-your-portal.htm), you can omit passing in the username and password. The ArcGIS API for Python is able to figure out when the GIS is using Windows authentication and picks the login credentials from the currently running process providing a seamless and secure login experience. > **Note:** SAML-compliant identity providers configured to use Kerberos or NTLM will not create this same seamless login experience.Windows authentication only works on the Windows OS and requires pywin32 and kerberos-sspi python packages.<jupyter_code>print("\n\nIntegrated Windows Authentication using NTLM or Kerberos")  
gis = GIS("https://portalname.domain.com/webcontext")
print("Logged in as: " + gis.properties.user.username)<jupyter_output>Integrated Windows Authentication using NTLM or Kerberos
Logged in as: username@DOMAIN<jupyter_text>User authentication with OAuth 2.0 The ArcGIS Python API supports [OAuth 2.0](https://developers.arcgis.com/documentation/core-concepts/security-and-authentication/what-is-oauth-2/) as an authentication method, and acts as a [serverless native application](https://developers.arcgis.com/documentation/core-concepts/security-and-authentication/mobile-and-native-user-logins/) when using OAuth 2.0 authorization with ArcGIS.To use this mode of authorization, you need a `client id`. If you already have a client id, you can skip the following section. Obtaining a client idThe steps below show how a client id can be obtained by registering a new application with your GIS. Only one client id is required, so if your GIS has one already, you may use it instead of creating a new application.    * Log into your web GIS (ArcGIS Online organization or ArcGIS Enterprise)* Go to Content tab* Click '+ Add Item > An Application' menu* Add an application: * Type: Application * Title: Python * Tags: python* On the Item details page of this newly created application, navigate to Settings tab* Click the Registered Info button. It's towards the bottom of the page.* This will dispaly an App ID. This is your client id and needs to be passed in as the client_id parameter when construcing a GIS object. You need this in your Python code to log in.    You can then log on to your org using the Python API using the code shown below:  Interactive Login ExperienceThis uses interactive sign-in experience: you would be redirected to your organization's sign-in page using the configured identity provider. Upon signing in, you would get a code that you can paste back to complete the sign-in process:<jupyter_code>gis = GIS("https://pythonapi.playground.esri.com/portal", client_id='f8cRxbP3NO8bf9ag')
print("Successfully logged in as: " + gis.properties.user.username)<jupyter_output>Please sign in to your GIS and paste the code that is obtained below.
If a web browser does not automatically open, please navigate to the URL below yourself instead.
Opening web browser to navigate to: https://pythonapi.playground.esri.com/portal/sharing/rest/oauth2/authorize?client_id=f8cRxbP3NO8bf9ag&response_type=code&expiration=-1&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob
Enter code obtained on signing in using SAML: ········
Successfully logged in as: arcgis_python<jupyter_text>Non-Interactive Login ExperienceThe recommended suggestion for non-interactive login scripts is to use the built-in identity provider instead of SAML.  Connecting through ArcGIS Pro It is often useful to write scripts that work against the active portal in the ArcGIS Pro app.Using the `pro` authentication scheme, scripts can create an instance of the `GIS` class representing the active portal in ArcGIS Pro without requiring the user to pass their credentials. In this mode, users can leverage the Pro app to login to the portal and their scripts can use whichever Portal is currently active. This mode can also serve as a bridge for users with advanced authentication scenarios like IWA using NTLM or Kerberos or Smart Card where signing in with credentials may not be possible or desirable.> **NOTE:** When using a [Named User license](https://pro.arcgis.com/en/pro-app/get-started/licensing-arcgis-pro.htm) to license ArcGIS Pro, unless the [`Sign me in automatically`](https://pro.arcgis.com/en/pro-app/help/projects/sign-in-to-your-organization.htm) checkbox is selected when signing into the licensing portal or ArcGIS Pro has been [licensed for offline use](https://pro.arcgis.com/en/pro-app/get-started/start-arcgis-pro-with-a-named-user-license.htmESRI_SECTION1_15AD453E27C446CE9B51D45C021E8067), ArcGIS Pro should be installed and concurrently running on the machine that executes the script for authentication to succeed. When selecting `Sign me in automatically`, ArcGIS Pro can remain closed for 2 weeks by default before the portal token expires. See [`here`](https://enterprise.arcgis.com/en/portal/latest/administer/windows/specify-the-default-token-expiration-time.htm) for details on configuring token settings.<jupyter_code>print("\n\nActive Portal in ArcGIS Pro")  
gis = GIS("pro")<jupyter_output>Active Portal in ArcGIS Pro<jupyter_text>Protecting your credentialsWhen sharing your notebooks with other users or if you are storing it in a public location you want to obfuscate your credentials stored in the notebook. You can do so by simply not providing the password. The Python API uses the `getpass` module internally and would request your password in such a way that your keystrokes would not be printed on the screen.<jupyter_code>gis = GIS("https://pythonapi.playground.esri.com/portal", username='arcgis_python')
print("Successfully logged in as: " + gis.properties.user.username)<jupyter_output>Enter password: ········
Successfully logged in as: arcgis_python<jupyter_text>This halts execution of the cell and waits for the user to input text into  text box. All keystrokes are masked with an asterisk or similar character. Upon entry, execution resumes. The value entered by the user can be stored in a variable as shown in the code above.This technique could be used with any authentication scheme that requires a password. You can choose to mask the username as well. The `getpass()` function can be used in a standalone Python script as well. When doing so, remember, the script has to be run interactively as it expects input from user at runtime.  Storing your credentials locallyIf you are signing in frequently to a particular GIS and would like to store the credentials locally on your computer, then you could do so using the `profile` parameter of your `GIS`.Persistent profiles for the GIS can be created by giving the GIS authorization credentials and specifying a profile name. The profile stores all of the authorization credentials (except the password) in the user’s home directory in an unencrypted config file named `.arcgisprofile.` The profile securely stores the password in an O.S. specific password manager through the [keyring](https://pypi.python.org/pypi/keyring) python module. (Note: Linux systems may need additional software installed and configured for proper security) Once a profile has been saved, passing the profile parameter by itself uses the authorization credentials saved in the configuration file/password manager by that profile name. Multiple profiles can be created and used in parallel.<jupyter_code>#Define 2 different profiles for two different urls
playground_gis = GIS(url="https://pythonapi.playground.esri.com/portal", username='arcgis_python', password='<PASSWORD>',
                     profile='python_playground_prof')
agol_gis = GIS(url="https://arcgis.com/", username='arcgis_python', password="<PASSWORD>",
    profile="AGOL_prof")
print("profile defined for {}".format(playground_gis))
print("profile defined for {}".format(agol_gis))<jupyter_output>profile defined for GIS @ https://pythonapi.playground.esri.com/portal
profile defined for GIS @ https://geosaurus.maps.arcgis.com<jupyter_text>To sign in during a different session, simply specify the profile file name. You can work with any number of connections concurrently, as the authentication credentials stored for each profile will be automatically loaded at runtime.<jupyter_code>def print_profile_info(gis):
    print("Successfully logged into '{}' via the '{}' user".format(
           gis.properties.portalHostname,
           gis.properties.user.username)) 

playground_gis = GIS(profile='python_playground_prof')
print_profile_info(playground_gis)

agol_gis = GIS(profile="AGOL_prof")
print_profile_info(agol_gis)<jupyter_output>Successfully logged into 'pythonapi.playground.esri.com/portal' via the 'arcgis_python' user
Successfully logged into 'www.arcgis.com' via the 'arcgis_python' user
{'fraction_non_alphanumeric': 0.07575853953310858, 'fraction_numerical': 0.005088734813307042, 'mean_word_length': 5.50206782464847, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 58, 'https://': 24, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '225292', 'license': 'Apache-2.0', 'n_tokens_mistral': 4413, 'n_tokens_neox': 4231, 'n_words': 1758, 'path': 'guide/03-the-gis/working-with-different-authentication-schemes.ipynb', 'repo_name': 'brke/ArcGIS', 'size': '15744'}