Authenticating to internal proxy (407)

4338
2
Jump to solution
06-10-2015 04:11 PM
JohnDye
Occasional Contributor III

So, this is literally my first day writing a single line of C# and working with the .NET SDK. The Developer documentation has been really good thus far and I've been able to understand everything pretty well.

Unfortunately, when I got towards the end of the third tutorial, I ran into a problem - my corporate proxy.

Since this is my first day working with C#, I'm not sure how to build a function to use IWA to authenticate to our internal proxy.

Any help or guidance is greatly appreciated.

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;


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://gis.kdc.capitalone.com/portal/sharing/rest");
                    // create the portal 
                    this.arcGISOnline = await ArcGISPortal.CreateAsync(portalUri); 
                }
                // create a variable to store search results
                IEnumerable<ArcGISPortalItem> results = null;
                if (this.ItemTypeCombobox.SelectedValue.ToString() == "BaseMap")
                {
                    // basemap search returns web maps that contain the basemap layer
                    var basemapSearch = await this.arcGISOnline.ArcGISPortalInfo.SearchBasemapGalleryAsync();
                    results = basemapSearch.Results;
                }
                else
                {
                    // 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 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 Atlas");
            }
        }
        
        // 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;
        }
    }
}
1 Solution

Accepted Solutions
JohnDye
Occasional Contributor III

Got it!
If your internal proxy is blocking a request you are sending outside the domain and you're getting a 407 error -  provided your internal proxy uses IWA, you can simply update your App.config XML to with the <system.net> tag and set the <defaultProxy useDefaultCredentials> property to True.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
  <system.net>
    <defaultProxy useDefaultCredentials="True" />
  </system.net>
</configuration>

View solution in original post

0 Kudos
2 Replies
JohnDye
Occasional Contributor III

Got it!
If your internal proxy is blocking a request you are sending outside the domain and you're getting a 407 error -  provided your internal proxy uses IWA, you can simply update your App.config XML to with the <system.net> tag and set the <defaultProxy useDefaultCredentials> property to True.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
  <system.net>
    <defaultProxy useDefaultCredentials="True" />
  </system.net>
</configuration>
0 Kudos
Mannus_Etten
New Contributor III

yeah this only works in .net framework and not in .net5

CEO the Right Direction BV/Portal Genius
0 Kudos