Access a web map that is secured with ArcGIS token-based 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
- Create an
AuthenticationChallengeHandler
using theDefaultAuthenticationChallengeHandler
to handle the challenges sent by the protected map service. - Set the
AuthenticationChallengeHandler
used by theAuthenticationManager
. - Create a
Portal
. - Create a
PortalItem
for the protected web map using thePortal
and Item ID of the protected map service. - Create a map to display in the
MapView
using thePortalItem
. - 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
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()
}
}