Select to view content in your preferred language

Load Secure webmap

949
5
12-16-2019 08:15 AM
LaurynasGedminas2
Regular Contributor

What is the proper way to load secure webmap?

so far this code does not work:

MapView {
    id: mapView
   anchors.fill: parent

   Credential{
      id: creds
      username: user
      password: password
   }

   Map {

   id: webMap
   initUrl: webmap_secure

   }

}

Tags (4)
0 Kudos
5 Replies
LucasDanzinger
Esri Frequent Contributor

Laurynas-

Because a Map can contain layers and tables that are also secured, you can't directly set a credential on the Map. Instead, you need to use our AuthenticationManager, which is a class that catches all security challenges and notifies the developer, allowing the developer to satisfy the challenge (pass in a credential to the request). This is discussed further here - Access the ArcGIS platform—ArcGIS Runtime SDK for Qt | ArcGIS for Developers 

Example code would be:

ApplicationWindow {
    id: appWindow
    width: 800
    height: 600    

    // add a mapView component
    MapView {
        id: mv
        anchors.fill: parent

        // add a map to the mapview
        Map {
            initUrl: "securedWebmapUrl"
        }
    }

    Connections {
        target: AuthenticationManager // singleton instance
        onAuthenticationChallenge: {
            console.log("authentication challenge for", challenge.requestUrl)

            if (challenge.authenticationChallengeType === Enums.AuthenticationChallengeTypeUsernamePassword) {
                var credential = ArcGISRuntimeEnvironment.createObject("Credential", {
                                                                           username: "username",
                                                                           password: "password"
                                                                       });
                challenge.continueWithCredential(credential);
            }
        }
    }
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

If you wanted to instead show a user a dialog to sign in, you could use our Toolkit component called AuthenticationView:

import QtQuick 2.6
import QtQuick.Controls 2.2
import QtPositioning 5.12
import Esri.ArcGISRuntime 100.6
import Esri.ArcGISRuntime.Toolkit.Dialogs 100.6

ApplicationWindow {
    id: appWindow
    width: 800
    height: 600

    // add a mapView component
    MapView {
        id: mv
        anchors.fill: parent

        // add a map to the mapview
        Map {
            initUrl: "secureWebmapUrl"
        }
    }

    AuthenticationView {
        authenticationManager: AuthenticationManager
    }
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
LaurynasGedminas2
Regular Contributor

Thank you for quick response,

could be it be:

var credential = ArcGISRuntimeEnvironment.createObject("Credential", {
                                   "token": token
                                    });

its not working right now.

Laurynas

0 Kudos
LucasDanzinger
Esri Frequent Contributor

I believe if you want to directly set the token string, you may need to also set the referer property - Credential QML Type | ArcGIS for Developers .

Are you re-generating tokens somewhere else in your app? If you provide username and password instead, the credentials will be stored in the CredentialCache, and expired tokens will be automatically handled in the API. With your workflow, if your token expires, you'll need to handle fetching a new token and providing it to the Runtime API

0 Kudos
LaurynasGedminas2
Regular Contributor

Modifying the AppStudio "Quick Report" App, they are storing the token as string. the token works properly if i add single feature layer with:

Credential{
                id: creds
                token: app.settings.value("token", "")
            }

            FeatureLayer {
                            id: featureLayer
                            // feature table
                            ServiceFeatureTable {
                                id: featureTable
                                url: app.featureLayerURL
  
                                credential: creds
                            }
            }
0 Kudos
LaurynasGedminas2
Regular Contributor

So, The code works if i use the username and the password from the Quick Report Template App, it does not work if i use token from the App:

Connections {
                target: AuthenticationManager // singleton instance
                onAuthenticationChallenge: {
                    console.log("LG-authentication challenge for", challenge.requestUrl)
                    var user = rot13(app.settings.value("username",""))
                    var password = rot13(app.settings.value("password",""));
                    var token = app.settings.value("token","");
                    if (challenge.authenticationChallengeType === Enums.AuthenticationChallengeTypeUsernamePassword) {

                        /*var credential = ArcGISRuntimeEnvironment.createObject("Credential", {
                                                                                   username: user,
                                                                                   password: password
                                                                               });*/


                        console.log(token);
                        var credential = ArcGISRuntimeEnvironment.createObject("Credential", {
                                                                               "token": token
                                                                               });
                        //credential.token = app.token
                        
                    }
                }
            }

AppStudio for ArcGIS

0 Kudos