Uses Windows credentials to access services hosted on a portal secured with Integrated Windows Authentication (IWA).

Use case
IWA, which is built into Microsoft Internet Information Server (IIS), works well for intranet applications, but isn’t always practical for internet apps.
How to use the sample
Enter the URL to your IWA-secured portal in the text field at the top of the screen and click “Search Secure”. You will be prompted for a username (including domain, such as username@DOMAIN or domain\username), and password. On Windows it will automatically use your current login credentials. If you authenticate successfully, portal item results will display in the combo box below. Select a web map item and click the “Load Web Map” button to display it in the map view.
How it works
- The
AuthenticationManagerobject is configured with a challenge handler that will prompt for a Windows login (username, password, and domain) if a secure resource is encountered. - When a search for portal items is performed against an IWA-secured portal, the challenge handler creates an
Credentialobject from the information entered by the user. - If the user authenticates, the search returns a list of web maps (
PortalItem) and the user can select one to display as aMap. - On some platforms, the current Windows account is used by default and a login prompt will not be shown if it can authenticate successfully.
Relevant API
- Authenticator
- Authentication::AuthenticationManager
- Authentication::PasswordCredential
- Portal
About the data
This sample searches for web map portal items on a secure portal. To successfully run the sample, you need:
- Access to a portal secured with Integrated Windows Authentication that contains one or more web map items.
- A login that grants you access to the portal.
Additional information
More information about IWA and its use with ArcGIS can be found at the following links:
Tags
authentication, security, Windows
Sample Code
// [WriteFile Name=IntegratedWindowsAuthentication, Category=CloudAndPortal]// [Legal]// Copyright 2019 Esri.//// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at// http://www.apache.org/licenses/LICENSE-2.0//// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.// [Legal]
#ifdef PCH_BUILD#include "pch.hpp"#endif // PCH_BUILD
// sample headers#include "IntegratedWindowsAuthentication.h"
// ArcGIS Maps SDK headers#include "Error.h"#include "Map.h"#include "MapQuickView.h"#include "MapTypes.h"#include "Portal.h"#include "PortalItem.h"#include "PortalItemListModel.h"#include "PortalQueryParametersForItems.h"#include "PortalQueryResultSetForItems.h"#include "PortalTypes.h"
// Qt headers#include <QFuture>
using namespace Esri::ArcGISRuntime;
IntegratedWindowsAuthentication::IntegratedWindowsAuthentication(QObject* parent /* = nullptr */): QObject(parent), m_map(new Map(BasemapStyle::ArcGISTopographic, this)), query(new PortalQueryParametersForItems()){ query->setTypes(QList<PortalItemType>() << PortalItemType::WebMap);}
IntegratedWindowsAuthentication::~IntegratedWindowsAuthentication() = default;
void IntegratedWindowsAuthentication::init(){ // Register C++ classes as QML types qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView"); qmlRegisterType<IntegratedWindowsAuthentication>("Esri.Samples", 1, 0, "IntegratedWindowsAuthenticationSample");}
MapQuickView* IntegratedWindowsAuthentication::mapView() const{ return m_mapView;}
// Set the view (created in QML)void IntegratedWindowsAuthentication::setMapView(MapQuickView* mapView){ if (!mapView || mapView == m_mapView) return;
m_mapView = mapView; m_mapView->setMap(m_map);
emit mapViewChanged();}
void IntegratedWindowsAuthentication::searchIwaSecurePortal(const QString& url){ if (m_iwaSecurePortal && m_iwaSecurePortal->loadStatus() == LoadStatus::FailedToLoad) { m_iwaSecurePortal = nullptr; } m_iwaSecurePortal = new Portal(url, true, this);
connect(m_iwaSecurePortal, &Portal::doneLoading, this, &IntegratedWindowsAuthentication::securePortalDoneLoading);
m_loadingIndicator = true; emit isLoadingChanged();
m_iwaSecurePortal->load();}
void IntegratedWindowsAuthentication::loadSelectedWebmap(int index){ if (!m_webmaps) return;
if (m_map) delete m_map;
m_map = new Map(m_webmaps->at(index), this);
m_mapView->setMap(m_map);}
void IntegratedWindowsAuthentication::errorAccepted(){ m_mapLoadError.clear(); emit mapLoadErrorChanged();}
QAbstractListModel* IntegratedWindowsAuthentication::webmapListModel() const{ return m_webmaps;}
void IntegratedWindowsAuthentication::securePortalDoneLoading(const Error& loadError){ if (!loadError.isEmpty()) { m_mapLoadError = loadError.message(); m_loadingIndicator = false;
if(m_webmaps) m_webmaps = nullptr;
emit webmapListModelChanged(); emit isLoadingChanged(); emit mapLoadErrorChanged(); return; }
m_iwaSecurePortal->findItemsAsync(*query).then(this, [this](PortalQueryResultSetForItems* result) { searchItemsCompleted(result); });}
void IntegratedWindowsAuthentication::searchItemsCompleted(PortalQueryResultSetForItems* result){ if(!result) return;
if(m_webmaps) m_webmaps = nullptr;
m_webmaps = result->itemResults(); emit webmapListModelChanged();
m_loadingIndicator = false; emit isLoadingChanged();}// [WriteFile Name=IntegratedWindowsAuthentication, Category=CloudAndPortal]// [Legal]// Copyright 2019 Esri.//// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at// http://www.apache.org/licenses/LICENSE-2.0//// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.// [Legal]
#ifndef INTEGRATEDWINDOWSAUTHENTICATION_H#define INTEGRATEDWINDOWSAUTHENTICATION_H
// Qt headers#include <QAbstractListModel>#include <QObject>
namespace Esri::ArcGISRuntime{class Map;class MapQuickView;class Portal;class PortalItem;class PortalQueryResultSetForItems;class PortalItemListModel;class PortalQueryParametersForItems;class PortalQueryResultSetForItems;class Error;}
Q_MOC_INCLUDE("MapQuickView.h")
class IntegratedWindowsAuthentication : public QObject{ Q_OBJECT
Q_PROPERTY(Esri::ArcGISRuntime::MapQuickView* mapView READ mapView WRITE setMapView NOTIFY mapViewChanged) Q_PROPERTY(QAbstractListModel* webmapListModel READ webmapListModel NOTIFY webmapListModelChanged) Q_PROPERTY(QString mapLoadError MEMBER m_mapLoadError NOTIFY mapLoadErrorChanged) Q_PROPERTY(bool isLoading MEMBER m_loadingIndicator NOTIFY isLoadingChanged)
public: explicit IntegratedWindowsAuthentication(QObject* parent = nullptr); ~IntegratedWindowsAuthentication();
static void init();
Q_INVOKABLE void searchIwaSecurePortal(const QString& url); Q_INVOKABLE void loadSelectedWebmap(int index); Q_INVOKABLE void errorAccepted(); QAbstractListModel* webmapListModel() const;
signals: void mapViewChanged(); void webmapListModelChanged(); void mapLoadErrorChanged(); void isLoadingChanged();
private slots: void securePortalDoneLoading(const Esri::ArcGISRuntime::Error& loadError); void searchItemsCompleted(Esri::ArcGISRuntime::PortalQueryResultSetForItems* result);
private: Esri::ArcGISRuntime::MapQuickView* mapView() const; void setMapView(Esri::ArcGISRuntime::MapQuickView* mapView);
Esri::ArcGISRuntime::Map* m_map = nullptr; Esri::ArcGISRuntime::MapQuickView* m_mapView = nullptr; Esri::ArcGISRuntime::PortalQueryResultSetForItems* m_webmapResults = nullptr; Esri::ArcGISRuntime::PortalItemListModel* m_webmaps = nullptr; Esri::ArcGISRuntime::PortalItem* m_selectedItem = nullptr; Esri::ArcGISRuntime::Portal* m_iwaSecurePortal = nullptr; Esri::ArcGISRuntime::PortalQueryParametersForItems* query = nullptr; QString m_mapLoadError; bool m_loadingIndicator = false;};
#endif // INTEGRATEDWINDOWSAUTHENTICATION_H// [WriteFile Name=IntegratedWindowsAuthentication, Category=CloudAndPortal]// [Legal]// Copyright 2019 Esri.//// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at// http://www.apache.org/licenses/LICENSE-2.0//// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.// [Legal]
import QtQuickimport QtQuick.Controlsimport QtQuick.Layoutsimport Esri.Samplesimport Esri.ArcGISRuntime.Toolkit
Item { // Declare the C++ instance which creates the map etc. and supply the view IntegratedWindowsAuthenticationSample { id: integratedWindowsAuthenticationSampleModel mapView: view }
MapView { id: view anchors.fill: parent
Component.onCompleted: { // Set the focus on MapView to initially enable keyboard navigation forceActiveFocus(); } }
Rectangle { id: connectionBox anchors { margins: 5 left: parent.left top: parent.top } width: 275 height: 175 color: "#000000" opacity: .70 radius: 5
// Prevent mouse interaction from propagating to the MapView MouseArea { anchors.fill: parent onPressed: mouse => mouse.accepted = true; onWheel: wheel => wheel.accepted = true; }
ColumnLayout { id: enterPortalPrompt anchors { fill: parent margins: 5 }
visible: !webmapsList.model
Text { text: qsTr("Enter portal URL secured by IWA") color: "white" font { bold: true pixelSize: 14 } }
TextField { id: securePortalUrl Layout.fillWidth: true Layout.margins: 2 selectByMouse: true
background: Rectangle { implicitWidth: parent.width implicitHeight: parent.height color: "white" } }
Row { Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter Layout.margins: 2 spacing: 3
Button { text: qsTr("Search Secure") onClicked: { if (securePortalUrl.text) { integratedWindowsAuthenticationSampleModel.searchIwaSecurePortal(securePortalUrl.text); } else { webMapMsg.text = "Portal URL is empty. Please enter a portal URL" webMapMsg.visible = true; return; } } } } }
ColumnLayout { id: selectMapPrompt anchors { fill: parent margins: 5 } visible: webmapsList.model
Text { id: header text: "Connected to:" Layout.fillWidth: true color: "white" font { bold: true pointSize: 14 } }
Text { id: portalName Layout.fillWidth: true text: securePortalUrl.text horizontalAlignment: Text.AlignLeft elide: Text.ElideMiddle color: "white" font { bold: true pointSize: 14 } }
ComboBox { id: webmapsList Layout.margins: 2 Layout.fillWidth: true textRole: qsTr("title") model: integratedWindowsAuthenticationSampleModel.webmapListModel }
Button { text: qsTr("Load Web Map") Layout.fillWidth: true Layout.margins: 2 visible: webmapsList.model onClicked: integratedWindowsAuthenticationSampleModel.loadSelectedWebmap(webmapsList.currentIndex); } } }
BusyIndicator { id: indicator anchors.centerIn: parent running: integratedWindowsAuthenticationSampleModel.isLoading }
// Declare Authenticator to handle any authentication challenges Authenticator { anchors.fill: parent }
Dialog { id: webMapMsg anchors.centerIn: parent property alias text : textLabel.text property alias informativeText : detailsLabel.text modal: true
standardButtons: Dialog.Ok title: "Could not load web map!" visible: integratedWindowsAuthenticationSampleModel.mapLoadError.length > 0 ColumnLayout { Text { id: textLabel text: integratedWindowsAuthenticationSampleModel.mapLoadError } Text { id: detailsLabel } } onAccepted: integratedWindowsAuthenticationSampleModel.errorAccepted(); }}// [Legal]// Copyright 2019 Esri.//// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at// http://www.apache.org/licenses/LICENSE-2.0//// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.// [Legal]
// sample headers#include "IntegratedWindowsAuthentication.h"
// ArcGIS Maps SDK headers#include "ArcGISRuntimeEnvironment.h"
// Qt headers#include <QDir>#include <QGuiApplication>#include <QQmlApplicationEngine>#include <QQmlEngine>
// Platform specific headers#ifdef Q_OS_WIN#include <Windows.h>#endif
#ifdef QT_WEBVIEW_WEBENGINE_BACKEND#include <QtWebEngineQuick>#endif // QT_WEBVIEW_WEBENGINE_BACKEND
#define STRINGIZE(x) #x#define QUOTE(x) STRINGIZE(x)
#include "Esri/ArcGISRuntime/Toolkit/register.h"
int main(int argc, char *argv[]){ Esri::ArcGISRuntime::ArcGISRuntimeEnvironment::setUseLegacyAuthentication(false);#ifdef QT_WEBVIEW_WEBENGINE_BACKEND QtWebEngineQuick::initialize();#endif // QT_WEBVIEW_WEBENGINE_BACKEND
QGuiApplication app(argc, argv); app.setApplicationName(QString("IntegratedWindowsAuthentication"));
// Use of ArcGIS location services, such as basemap styles, geocoding, and routing services, // requires an access token. For more information see // https://links.esri.com/arcgis-runtime-security-auth.
// The following methods grant an access token:
// 1. User authentication: Grants a temporary access token associated with a user's ArcGIS account. // To generate a token, a user logs in to the app with an ArcGIS account that is part of an // organization in ArcGIS Online or ArcGIS Enterprise.
// 2. API key authentication: Get a long-lived access token that gives your application access to // ArcGIS location services. Go to the tutorial at https://links.esri.com/create-an-api-key. // Copy the API Key access token.
const QString accessToken = QString("");
if (accessToken.isEmpty()) { qWarning() << "Use of ArcGIS location services, such as the basemap styles service, requires" << "you to authenticate with an ArcGIS account or set the API Key property."; } else { Esri::ArcGISRuntime::ArcGISRuntimeEnvironment::setApiKey(accessToken); }
// Initialize the sample IntegratedWindowsAuthentication::init();
QString arcGISRuntimeImportPath = QUOTE(ARCGIS_RUNTIME_IMPORT_PATH);
#if defined(LINUX_PLATFORM_REPLACEMENT) // on some linux platforms the string 'linux' is replaced with 1 // fix the replacement paths which were created QString replaceString = QUOTE(LINUX_PLATFORM_REPLACEMENT); arcGISRuntimeImportPath = arcGISRuntimeImportPath.replace(replaceString, "linux", Qt::CaseSensitive);#endif
// Initialize application view QQmlApplicationEngine engine; // Add the import Path engine.addImportPath(QDir(QCoreApplication::applicationDirPath()).filePath("qml")); // Add the Runtime and Extras path engine.addImportPath(arcGISRuntimeImportPath);
Esri::ArcGISRuntime::Toolkit::registerComponents(engine);
// Set the source engine.load(QUrl("qrc:/Samples/CloudAndPortal/IntegratedWindowsAuthentication/main.qml"));
return app.exec();}// Copyright 2019 ESRI//// All rights reserved under the copyright laws of the United States// and applicable international laws, treaties, and conventions.//// You may freely redistribute and use this sample code, with or// without modification, provided you include the original copyright// notice and use restrictions.//// See the Sample code usage restrictions document for further information.//
import QtQuick.Controlsimport Esri.Samples
ApplicationWindow { visible: true width: 800 height: 600
IntegratedWindowsAuthentication { anchors.fill: parent }}