AnsweredAssumed Answered

Authenticating to Portal

Question asked by johnmdye on Jun 11, 2015
Latest reply on Jun 15, 2015 by johnmdye

Can someone provide an example of how I have to create the ChallengeHandler to allow a user to authenticate to an On-Premise Portal?

I'm creating my FirstApp and while I can get it to work with ArcGIS Online, I can't get it to connect to our own Portal when I change the portalUri

XAML.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Esri.ArcGISRuntime.Controls;
using Esri.ArcGISRuntime.Portal;
using Esri.ArcGISRuntime.WebMap;
using Esri.ArcGISRuntime.Security;


namespace FirstApp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>


    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private ArcGISPortal arcGISOnline;
        private ArcGISPortalItem selectedPortalItem;


        // changes the basemap layer
        private void baseMap_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var combo = sender as ComboBox;
            var sel = combo.SelectedItem as ComboBoxItem;
            if (sel.Tag == null) { return; }


            //Find and remove the current basemap layer from the map
            if (mainMap == null) { return; }
            var oldBasemap = mainMap.Layers["BaseMap"];
            mainMap.Layers.Remove(oldBasemap);


            //Create a new basemap layer
            var newBaseMap = new Esri.ArcGISRuntime.Layers.ArcGISTiledMapServiceLayer();
            //Set the ServiceUri with the url defined for the ComboboxItem's Tag
            newBaseMap.ServiceUri = sel.Tag.ToString();
            //Give the layer an ID so it an still be found with the code above
            newBaseMap.ID = "BaseMap";
            
            //Insert the new basemap layer as the first (bottom) layer in the map
            mainMap.Layers.Insert(0, newBaseMap);
        }


        // searches the portal for content
        private async void SearchButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (this.arcGISOnline == null) 
                {
                    // create the uri for the portal 
                    var portalUri = new Uri("https://myserver.domain.com/portal/sharing/rest");
                    // create the portal 
                    this.arcGISOnline = await ArcGISPortal.CreateAsync(portalUri); 
                }
                // create a variable to store search results
                IEnumerable<ArcGISPortalItem> results = null;
                // get the search term provided in the UI
                var searchTerm = this.SearchTextBox.Text.Trim();
                var searchItem = this.ItemTypeCombobox.SelectedValue.ToString();


                // build a query that searches for the specified searchItem type
                // ('web mapping application' is excluded from the search since 'web map' will match those item types too)
                var queryString = string.Format("\"{0}\" type:(\"{1}\" NOT \"web mapping application\")", searchTerm, searchItem);
                var searchParameters = new SearchParameters()
                {
                    QueryString = queryString,
                    SortField = "avgrating",
                    SortOrder = QuerySortOrder.Descending,
                    Limit = 10
                };
                // execute the search
                var itemSearch = await this.arcGISOnline.SearchItemsAsync(searchParameters);
                results = itemSearch.Results;


                // show the results in the list box
                this.ResultListBox.ItemsSource = results;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error searching Portal");
            }
        }
        
        // resets the UI controls to keep them in sync with the currently selected portal item in the ResultsListBox
        private void ResetUI()
        {
            // clear UI controls
            this.SnippetTextBlock.Text = "";
            this.ThumbnailImage.Source = null;
            this.ShowMapButton.IsEnabled = false;
        }


        private void ResultSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.ResetUI();


            // store the currently selected portal item
            this.selectedPortalItem = this.ResultListBox.SelectedItem as ArcGISPortalItem;
            if (this.selectedPortalItem == null) { return; }


            // show the portal item snippet (brief description) in the UI
            if (!string.IsNullOrEmpty(this.selectedPortalItem.Snippet))
            {
                this.SnippetTextBlock.Text = this.selectedPortalItem.Snippet;
            }


            // show a thumnail for the selected portal item (if there is one)
            if (this.selectedPortalItem.ThumbnailUri != null)
            {
                var src = new BitmapImage(this.selectedPortalItem.ThumbnailUri);
                this.ThumbnailImage.Source = src;
            }


            // enable the show map button when a web map portal item is chosen
            this.ShowMapButton.IsEnabled = (this.selectedPortalItem.Type == ItemType.WebMap);
        }


        private async void ShowMapButton_Click(object sender, RoutedEventArgs e)
        {
            // create a web map from the selected portal item
            var webMap = await WebMap.FromPortalItemAsync(this.selectedPortalItem);


            // load the web map into a web map view model
            var webMapVM = await WebMapViewModel.LoadAsync(webMap, this.arcGISOnline);


            // show the web map view model's map in the page's map view control
            this.mainMapView.Map = webMapVM.Map;
        }
    }
}

Outcomes