0

I cannot get a Scene Service to load into my Runtime app. It is a simple app with a scene and a Layer list. It is 99% the same as the code in the "Token Authentication Challenge" sample app. I have changed the XAML to display a SceneView and my Scene-Service using an ArcGISSceneLayer layer. The topo layer displays as 'Access Granted' however my layer shows 'Access Denied'. I am using ArcGIS Runtime 100.0 and Visual Studio 2013. Whilst rejigging the code I also saw the error 'key not found'. That code is further below ('XAML 2' and 'C# 2').

Code below:

XAML:

<Window x:Class="TokenSecuredServices.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:esri="http://schemas.esri.com/arcgis/runtime/2013"
    Title="Token Secured Services (Challenge)">

<Grid x:Name="layoutGrid">
    <esri:SceneView x:Name="MySceneView">
        <esri:Scene>
            <!-- This layer is public and does not require credentials -->
            <esri:ArcGISTiledLayer Name="World Street Map - Public"
                Source="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer" />
            <!-- This layer is secured with ArcGIS tokens and requires a login -->
            <!--username: user1 | password: user1-->
            <esri:ArcGISSceneLayer Name="AGOL Scene Service"
                Source="https://tiles.arcgis.com/tiles/*********/arcgis/rest/services/*********/SceneServer" />
        </esri:Scene>
    </esri:SceneView>

    <!-- Login UI -->
    <Border x:Name="loginPanel"
            Background="White" BorderBrush="Black" BorderThickness="1" 
            Margin="30" Padding="20"
            HorizontalAlignment="Center" VerticalAlignment="Top" 
            Visibility="Collapsed">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="100"/>
                <ColumnDefinition Width="150" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>

            <TextBlock Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" 
                       Margin="0,0,0,8"
                       TextWrapping="Wrap" 
                       Text="{Binding ServiceUrl, StringFormat='Login to: {0}'}" />
            <TextBlock Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" 
                       Margin="0,0,0,8"
                       TextWrapping="Wrap" 
                       Text="Username and Password are user1/user1" />
            <TextBlock Grid.Row="2" Grid.Column="0" 
                       Margin="2" 
                       VerticalAlignment="Center" 
                       Text="Username" />
            <TextBox Grid.Row="2" Grid.Column="1" 
                     Margin="2" 
                     Text="{Binding UserName, UpdateSourceTrigger=PropertyChanged}" />
            <TextBlock Grid.Row="3" Grid.Column="0" 
                       Margin="2" 
                       VerticalAlignment="Center" 
                       Text="Password" />
            <!-- For simplicity, the password is visible in this example. You can use a PasswordBox to mask the password text,
                 but it complicates data binding. See this discussion for details: http://stackoverflow.com/questions/1483892/how-to-bind-to-a-passwordbox-in-mvvm -->
            <TextBox Grid.Row="3" Grid.Column="1" 
                     Margin="2" 
                     Text="{Binding Password, UpdateSourceTrigger=PropertyChanged}" />
            <Button Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2" 
                    Margin="0,12,0,0" 
                    HorizontalAlignment="Center" 
                    Content="Login and Load Layer" 
                    IsDefault="True"
                    Click="LoginButtonClick" />
            <TextBlock Grid.Row="5" Grid.Column="0" Grid.ColumnSpan="2" 
                       Margin="8,12,8,0" 
                       Foreground="Red" 
                       TextWrapping="Wrap" 
                       Text="{Binding ErrorMessage}">
                <TextBlock.Style>
                    <Style TargetType="{x:Type TextBlock}">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding ErrorMessage}" Value="">
                                <Setter Property="Visibility" Value="Collapsed" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </TextBlock.Style>
            </TextBlock>
        </Grid>
    </Border>

    <!-- Layer listing with status -->
    <Border Background="White" BorderBrush="Black" BorderThickness="2" 
            Margin="30" 
            MaxWidth="500"
            HorizontalAlignment="Right" VerticalAlignment="Top">
        <StackPanel>
            <TextBlock FontSize="14" FontWeight="Bold"
                       Margin="4" 
                       Text="Map Layers (with status)" />
            <ListView x:Name="listLayerStatus" 
                      Padding="6" 
                      ItemsSource="{Binding ElementName=MySceneView, Path=Scene.AllLayers}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock Text="{Binding Name}" 
                                       HorizontalAlignment="Left" />
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Margin="20,0,0,5" 
                                           FontWeight="Bold">
                                    <TextBlock.Style>
                                        <Style TargetType="{x:Type TextBlock}">
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding LoadStatus}" 
                                                             Value="2">
                                                    <Setter Property="Foreground" Value="Red" />
                                                    <Setter Property="Text" Value="Access Denied" />
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding LoadStatus}" Value="0">
                                                    <Setter Property="Foreground" Value="Green" />
                                                    <Setter Property="Text" Value="Access Granted" />
                                                </DataTrigger>
                                            </Style.Triggers>

                                            <Style.Setters>
                                                <Setter Property="Foreground" Value="Black" />
                                                <Setter Property="Text" Value="Initializing" />
                                            </Style.Setters>
                                        </Style>
                                    </TextBlock.Style>
                                </TextBlock>
                            </StackPanel>
                        </StackPanel>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackPanel>
    </Border>
</Grid>

C#:

using Esri.ArcGISRuntime.Security;
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Windows;

namespace TokenSecuredServices
{
    public partial class MainWindow : Window
    {
        // Task completion source to track a login attempt
        private TaskCompletionSource<Credential> _loginTaskCompletionSource;

        public MainWindow()
        {
            InitializeComponent();

            // Define a method to challenge the user for credentials when a secured resource is encountered
            AuthenticationManager.Current.ChallengeHandler = new ChallengeHandler(Challenge);
        }

        private async Task<Credential> Challenge(CredentialRequestInfo info)
        {
            // Call code to get user credentials
            // Make sure it runs on the UI thread
            if (this.Dispatcher == null)
            {
                // No current dispatcher, code is already running on the UI thread
                return await GetUserCredentialsFromUI(info);
            }
            else
            {
                // Use the dispatcher to invoke the challenge UI
                return await this.Dispatcher.Invoke(() => GetUserCredentialsFromUI(info));
            }
        }

        private async Task<Credential> GetUserCredentialsFromUI(CredentialRequestInfo info)
        {
            // Show the login UI
            try
            {
                // Create a new LoginInfo to store the entered username and password
                // Pass the CredentialRequestInfo object so the resource URI can be stored
                var loginInputInfo = new LoginInfo(info);

                // Set the login UI data context with the LoginInfo
                loginPanel.DataContext = loginInputInfo;

                // Show the login UI
                loginPanel.Visibility = Visibility.Visible;

                // Create a new task completion source to return the user's login when complete
                // Set the login UI data context (LoginInfo object) as the AsyncState so it can be retrieved later
                _loginTaskCompletionSource = new TaskCompletionSource<Credential>(loginPanel.DataContext);

                // Return the task from the completion source
                // When the login button on the UI is clicked, the info will be returned for creating the credential
                return await _loginTaskCompletionSource.Task;
            }
            finally
            {
                // Hide the login UI
                loginPanel.Visibility = Visibility.Collapsed;
            }
        }

        // Handle the click event for the login button on the login UI
        private async void LoginButtonClick(object sender, RoutedEventArgs e)
        {
            // Make sure there's a task completion source for the login operation
            if (_loginTaskCompletionSource == null || _loginTaskCompletionSource.Task == null || _loginTaskCompletionSource.Task.AsyncState == null)
            {
                return;
            }

            // Get the login info from the task completion source
            var loginEntry = _loginTaskCompletionSource.Task.AsyncState as LoginInfo;

            try
            {
                // Create a token credential using the provided username and password
                TokenCredential userCredentials = await AuthenticationManager.Current.GenerateCredentialAsync
                                            (new Uri(loginEntry.ServiceUrl),
                                             loginEntry.UserName,
                                             loginEntry.Password,
                                             loginEntry.RequestInfo.GenerateTokenOptions);

                // Set the result on the task completion source
                _loginTaskCompletionSource.TrySetResult(userCredentials);
            }
            catch (Exception ex)
            {
                // Show exceptions on the login UI
                loginEntry.ErrorMessage = ex.Message;

                // Increment the login attempt count
                loginEntry.AttemptCount++;

                // Set an exception on the login task completion source after three login attempts
                if (loginEntry.AttemptCount >= 3)
                {
                    // This causes the login attempt to fail
                    _loginTaskCompletionSource.TrySetException(ex);
                }
            }
        }
    }

    // Helper class to contain login information
    internal class LoginInfo : INotifyPropertyChanged
    {
        // Information about the current request for credentials 
        private CredentialRequestInfo _requestInfo;
        public CredentialRequestInfo RequestInfo
        {
            get { return _requestInfo; }
            set
            {
                _requestInfo = value;
                OnPropertyChanged();
            }
        }

        // URI for the service that is requesting credentials
        private string _serviceUrl;
        public string ServiceUrl
        {
            get { return _serviceUrl; }
            set
            {
                _serviceUrl = value;
                OnPropertyChanged();
            }
        }

        // Username entered by the user
        private string _userName;
        public string UserName
        {
            get { return _userName; }
            set
            {
                _userName = value;
                OnPropertyChanged();
            }
        }

        // Password entered by the user
        private string _password;
        public string Password
        {
            get { return _password; }
            set
            {
                _password = value;
                OnPropertyChanged();
            }
        }

        // Last error message encountered while creating credentials
        private string _errorMessage;
        public string ErrorMessage
        {
            get { return _errorMessage; }
            set
            {
                _errorMessage = value;
                OnPropertyChanged();
            }
        }

        // Number of login attempts
        private int _attemptCount;
        public int AttemptCount
        {
            get { return _attemptCount; }
            set
            {
                _attemptCount = value;
                OnPropertyChanged();
            }
        }

        // Store the credential request information when the class is constructed
        public LoginInfo(CredentialRequestInfo info)
        {
            RequestInfo = info;
            ServiceUrl = info.ServiceUri.GetLeftPart(UriPartial.Path);
            ErrorMessage = string.Empty;
            AttemptCount = 0;
        }

        // Raise a property changed event so bound controls can update
        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

XAML 2:

<Controls:MetroWindow x:Class="Scene_Layer_Test.MainWindow"
                  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                  xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
                  xmlns:esri="http://schemas.esri.com/arcgis/runtime/2013" 
                  Title="MainWindow"
                  WindowState="Maximized"
                  xmlns:c="clr-namespace:Scene_Layer_Test"
                  Height="573"
                  Width="789.375"
                  x:Name="SceneWindow">
<Grid Background="Black" Margin="0,0,0,11">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="461*"/>
        <ColumnDefinition Width="320*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="247*"/>
        <RowDefinition Height="99*"/>
    </Grid.RowDefinitions>
    <esri:SceneView Margin="10" BorderBrush="White" BorderThickness="3" Name="MySceneView" />

    <Button x:Name="LoadSFBuildings" Content="Load New Scene" HorizontalAlignment="Left" Margin="2,10,0,0" VerticalAlignment="Top" Width="189" Click="LoadNewScene_Click" Grid.Column="1" Height="27"/>
    <Label Content="New Scene Load Status: " Name="LoadStatusLabel"  HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,0,0,0" Grid.Row="1" Width="179" Foreground="White" Height="29"/>
    <TextBlock Text="No Scene Available Yet" HorizontalAlignment="Left" Margin="194,5,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Width="215" Height="24" Foreground="White" Name="LoadStatusTextBlock"/>
    <Button x:Name="LoadAuckLayer" Content="Load LAS Scene Service" Grid.Column="1" HorizontalAlignment="Left" Margin="0,42,0,0" VerticalAlignment="Top" Width="191" Click="LoadAuckLayer_Click" Height="27"/>
    <Label Content="Scene Service Load Status: " x:Name="LoadStatusLabel_Copy"  HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,34,0,0" Grid.Row="1" Width="179" Foreground="White" Height="29"/>
    <TextBlock Text="No Layer Available Yet" HorizontalAlignment="Left" Margin="194,39,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Width="215" Height="24" Foreground="White" x:Name="AuckBuildLoadStatTextBlock"/>
</Grid>

C# 2:

using Esri.ArcGISRuntime.Geometry;
using System.Threading;
using Esri.ArcGISRuntime.Location;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Esri.ArcGISRuntime.Security;

namespace Scene_Layer_Test
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : MetroWindow
    {

        public MainWindow()
        {
            InitializeComponent();
        }


        private void LoadNewScene_Click(object sender, RoutedEventArgs e)
        {
            Uri bmUri = new Uri("http://services.arcgisonline.com/arcgis/rest/services/World_Topo_Map/MapServer");
            ArcGISTiledLayer bmTLayer = new ArcGISTiledLayer(bmUri);
            Basemap bMap = new Basemap(bmTLayer);

            Scene newScene = new Scene(bMap);

            MapPoint mPt = new MapPoint(144.970782, -37.828037, 50000, SpatialReferences.Wgs84);
            Viewpoint vPt = new Viewpoint(mPt, 1);

            newScene.InitialViewpoint = vPt;

            newScene.LoadStatusChanged +=newScene_LoadStatusChanged;

            MySceneView.Scene = newScene;
        }

        void newScene_LoadStatusChanged(object sender, LoadStatusEventArgs e)
        {
            LoadStatusTextBlock.Text = e.Status.ToString();
        }

        public async void LoadAuckLayer_Click(object sender, RoutedEventArgs e)
        {

            //SceneServer LAS Uri
            Uri ssLasUri = new Uri("https://tiles.arcgis.com/tiles/*******/arcgis/rest/services/*****/SceneServer");

            ArcGISSceneLayer sfSceneLayer = new ArcGISSceneLayer(ssLasUri);
            sfSceneLayer.Name = "AuckBuild";

            sfSceneLayer.LoadStatusChanged += sfSceneLayer_LoadStatusChanged;

            await sfSceneLayer.LoadAsync();

            MySceneView.Scene.OperationalLayers.Add(sfSceneLayer);
        }

        void sfSceneLayer_LoadStatusChanged(object sender, LoadStatusEventArgs e)
        {
            AuckBuildLoadStatTextBlock.Text = e.Status.ToString();
        }
    }
}
1

I think you just need to use layer endpoint ("/layer/[id]") For example: http://tiles.arcgis.com/tiles/z2tnIkrLQ2BRzr6P/arcgis/rest/services/RiparianAfter/SceneServer/layers/0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.