Token authentication

View inJavaKotlinView on GitHubSample viewer app

Access a web map that is secured with ArcGIS token-based authentication.

Image of token authentication

Use case

Allows you to access a secure service with the convenience and security of ArcGIS token-based authentication. For example, rather than providing a user name and password every time you want to access a secure service, you only provide those credentials initially to obtain a token which can then be used to access secured resources.

How to use the sample

Once you launch the app, you will be challenged for an ArcGIS Online login to view the protected map service. Enter a user name and password for an ArcGIS Online named user account (such as your ArcGIS for Developers account). If you authenticate successfully, the protected map service will display in the map.

How it works

  1. Create an AuthenticationChallengeHandler using the DefaultAuthenticationChallengeHandler to handle the challenges sent by the protected map service.
  2. Set the AuthenticationChallengeHandler used by the AuthenticationManager.
  3. Create a Portal.
  4. Create a PortalItem for the protected web map using the Portal and Item ID of the protected map service.
  5. Create a map to display in the MapView using the PortalItem.
  6. Set the map to display in the MapView.

Relevant API

  • AuthenticationChallengeHandler
  • AuthenticationManager
  • DefaultAuthenticationChallengeHandler
  • Map
  • MapView
  • Portal
  • PortalItem

About the data

The Traffic web map uses public layers as well as the world traffic (premium content) layer. The world traffic service presents historical and near real-time traffic information for different regions in the world. The data is updated every 5 minutes. This map service requires an ArcGIS Online organizational subscription.

Tags

authentication, cloud, portal, remember, security

Sample Code

MainActivity.kt
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.esri.arcgisruntime.sample.tokenauthentication

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.esri.arcgisruntime.mapping.ArcGISMap
import com.esri.arcgisruntime.mapping.view.MapView
import com.esri.arcgisruntime.portal.Portal
import com.esri.arcgisruntime.portal.PortalItem
import com.esri.arcgisruntime.sample.tokenauthentication.databinding.ActivityMainBinding
import com.esri.arcgisruntime.security.AuthenticationManager
import com.esri.arcgisruntime.security.DefaultAuthenticationChallengeHandler

class MainActivity : AppCompatActivity() {

    private val activityMainBinding by lazy {
        ActivityMainBinding.inflate(layoutInflater)
    }

    private val mapView: MapView by lazy {
        activityMainBinding.mapView
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(activityMainBinding.root)

        // set up an authentication handler to take credentials for access to the protected map service
        AuthenticationManager.setAuthenticationChallengeHandler(
            DefaultAuthenticationChallengeHandler(
                this
            )
        )

        // create a portal to ArcGIS Online
        val portal = Portal(getString(R.string.arcgis_online_portal_url))
        // create a portal item using the portal and the item id of a protected map service
        ArcGISMap(PortalItem(portal, getString(R.string.map_service_world_traffic_id))).let {
            // set the map to be displayed in the map view
            mapView.map = it
        }
    }

    override fun onResume() {
        super.onResume()
        mapView.resume()
    }

    override fun onPause() {
        mapView.pause()
        super.onPause()
    }

    override fun onDestroy() {
        mapView.dispose()
        super.onDestroy()
    }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.