+ Reply to Thread
Results 1 to 4 of 4

Thread: What is required for QueryTask Credentials ?

  1. #1
    Matt Miley
    Join Date
    Feb 2011
    Posts
    101
    Points
    2
    Answers Provided
    3


    0

    Default What is required for QueryTask Credentials ?

    Hello

    I'm trying to do a query during a Periodic Background Agent on a Secure Service and would like to just get the credentials from the app's local storage after the user has already sign in at least once.

    Here is my code..
    Code:
    QueryTask queryTask =
                    new QueryTask("http://servicesbeta4.esri.com/arcgis/rest/services/SaveTheBay/FeatureServer/0");
                queryTask.Credentials = WHAT IS REQUIRED HERE????
                queryTask.ExecuteCompleted += QueryTask_ExecuteCompleted;
    
                ESRI.ArcGIS.Client.Tasks.Query query = new ESRI.ArcGIS.Client.Tasks.Query();
                query.Where = "Confirmed = '1'";
    
                // This query will just populate the drop-down, so no need to return geometry
                query.ReturnGeometry = false;
                query.OutFields.Add("*");
                queryTask.ExecuteAsync(query);
    What comes after
    Code:
    queryTask.Credentials =
    I know the username and password is "user1", but I just don't know how to submit that in code.

    Thanks

  2. #2
    Jennifer Nery

    Join Date
    May 2010
    Posts
    2,449
    Points
    289
    Answers Provided
    51


    1
    This post is marked as the answer

    Default Re: What is required for QueryTask Credentials ?

    using System.Net;
    Code:
                queryTask.Credentials = new NetworkCredential("userName", "password", "domain");
    Jennifer Nery
    Software Developer
    ArcGIS API for SL/WPF/WP7

  3. #3
    Matt Miley
    Join Date
    Feb 2011
    Posts
    101
    Points
    2
    Answers Provided
    3


    0

    Default Re: What is required for QueryTask Credentials ?

    Thanks Jennifer

    Unfortunately that wasn't the problem though. I don't think i can do a query for attributes during a periodic task because its on the UI thread according to these responses from other people that got the same error.

    I keep getting "Invalid cross thread access exception" when it hits
    Code:
    QueryTask queryTask =
                    new QueryTask("http://servicesbeta4.esri.com/arcgis/rest/services/SaveTheBay/FeatureServer/0");
    and found these links for solutions http://stackoverflow.com/questions/8...-periodic-task

    I don't have much time to mess with it, is it possible to run a attribute query during a periodic task?

    Code:
    using Microsoft.Phone.Controls;
    using Microsoft.Phone.Shell;
    using System.Collections.Generic;
    using System;
    using System.Linq;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;
    using System.Windows.Data;
    using ESRI.ArcGIS.Client;
    using ESRI.ArcGIS.Client.Geometry;
    using ESRI.ArcGIS.Client.Symbols;
    using ESRI.ArcGIS.Client.FeatureService;
    using ESRI.ArcGIS.Client.Tasks;
    using Microsoft.Phone.Scheduler;
    using System.Net;
    
    namespace SeaLifeScheduledTask
    {
    
        public class ScheduledAgent : ScheduledTaskAgent
        {
    
            private static volatile bool _classInitialized;
    
            // Activate identity manager.
            /// <remarks>
            /// ScheduledAgent constructor, initializes the UnhandledException handler
            /// </remarks>
            public ScheduledAgent()
            {
    
                IdentityManager.Current.ChallengeMethod = Challenge;
                
                //ORGINIAL CODE HERE
                if (!_classInitialized)
                {
                    _classInitialized = true;
                    // Subscribe to the managed exception handler
                    Deployment.Current.Dispatcher.BeginInvoke(delegate
                    {
                        Application.Current.UnhandledException += ScheduledAgent_UnhandledException;
                    });
                }
            }
    
            
    
            /// Code to execute on Unhandled Exceptions
            private void ScheduledAgent_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
            {
                if (System.Diagnostics.Debugger.IsAttached)
                {
                    // An unhandled exception has occurred; break into the debugger
                    System.Diagnostics.Debugger.Break();
                }
            }
    
    
            /// <summary>
            /// Agent that runs a scheduled task
            /// </summary>
            /// <param name="task">
            /// The invoked task
            /// </param>
            /// <remarks>
            /// This method is called when a periodic or resource intensive task is invoked
            /// </remarks>
            protected override void OnInvoke(ScheduledTask task)
            {
                //TODO: Add code to perform your task in background
                IdentityManager.Current.ChallengeMethod = Challenge;
                
                //Intial Query to get scheduled Jobs
                QueryTask queryTask =
                    new QueryTask("http://servicesbeta4.esri.com/arcgis/rest/services/SaveTheBay/FeatureServer/0");
                queryTask.Credentials = new NetworkCredential("user1", "user1", "servicesbeta4.esri.com"); 
                queryTask.ExecuteCompleted += QueryTask_ExecuteCompleted;
    
                ESRI.ArcGIS.Client.Tasks.Query query = new ESRI.ArcGIS.Client.Tasks.Query();
                query.Where = "Confirmed = '1'";
    
                // This query will just populate the drop-down, so no need to return geometry
                query.ReturnGeometry = false;
                query.OutFields.Add("*");
                queryTask.ExecuteAsync(query);
            }
    
            private void Challenge(string url,
                          IdentityManager.CredentialCompleteHandler callback,
                          IdentityManager.GenerateTokenOptions options)
            {
                // Pop-up dialog box to enter credentials - user action to call GenerateCredentialAsync. 
                IdentityManager.Current.GenerateCredentialAsync(url, "user1", "user1",
                    (credential, ex) =>
                    // lambda used to create anonymous block so call to GenerateCredentialAsync is self-contained.
                    {
                        callback(credential, ex);
                    },
                    options);                 
            }
    
            void QueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs args)
            {
                FeatureSet featureSet = args.FeatureSet;
    
                //Get Total Jobs in Que
                int JobCount = featureSet.Features.Count;
                //string LastJobType = featureSet.Features
    
                ShellTile PrimaryTile = ShellTile.ActiveTiles.First();
    
                if (PrimaryTile != null)
                {
                    StandardTileData tile = new StandardTileData();
    
                    //tile.BackgroundImage = new Uri("images/sadface.png", UriKind.Relative);
                    tile.Count = JobCount;
                    tile.Title = "added by background agent";
                    tile.BackTitle = "Get to work dude";
                    tile.BackContent = "Buddy";
                    PrimaryTile.Update(tile);
                }
    
                NotifyComplete();
            }
        }
    }

  4. #4
    Matt Miley
    Join Date
    Feb 2011
    Posts
    101
    Points
    2
    Answers Provided
    3


    0

    Default Re: What is required for QueryTask Credentials ?

    Yay I got it to work see attached for for querying during an background agent on windows phone

    Code:
    using Microsoft.Phone.Controls;
    using Microsoft.Phone.Shell;
    using System.Collections.Generic;
    using System;
    using System.Linq;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;
    using System.Windows.Data;
    using ESRI.ArcGIS.Client;
    using ESRI.ArcGIS.Client.Geometry;
    using ESRI.ArcGIS.Client.Symbols;
    using ESRI.ArcGIS.Client.FeatureService;
    using ESRI.ArcGIS.Client.Tasks;
    using Microsoft.Phone.Scheduler;
    using System.Net;
    
    namespace SeaLifeScheduledTask
    {
    
        public class ScheduledAgent : ScheduledTaskAgent
        {
    
            private static volatile bool _classInitialized;
    
            // Activate identity manager.
            /// <remarks>
            /// ScheduledAgent constructor, initializes the UnhandledException handler
            /// </remarks>
            public ScheduledAgent()
            {
    
                IdentityManager.Current.ChallengeMethod = Challenge;
                
                //ORGINIAL CODE HERE
                if (!_classInitialized)
                {
                    _classInitialized = true;
                    // Subscribe to the managed exception handler
                    Deployment.Current.Dispatcher.BeginInvoke(delegate
                    {
                        //Intial Query to get scheduled Jobs
                        QueryTask queryTask =
                            new QueryTask("http://servicesbeta4.esri.com/arcgis/rest/services/SaveTheBay/FeatureServer/0");
                        queryTask.Credentials = new NetworkCredential("user1", "user1", "servicesbeta4.esri.com");
                        queryTask.ExecuteCompleted += QueryTask_ExecuteCompleted;
    
                        ESRI.ArcGIS.Client.Tasks.Query query = new ESRI.ArcGIS.Client.Tasks.Query();
                        query.Where = "Confirmed = '1'";
    
                        // This query will just populate the drop-down, so no need to return geometry
                        query.ReturnGeometry = false;
                        query.OutFields.Add("*");
                        queryTask.ExecuteAsync(query);
                        Application.Current.UnhandledException += ScheduledAgent_UnhandledException;
                    });
                }
            }
    
            
    
            /// Code to execute on Unhandled Exceptions
            private void ScheduledAgent_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
            {
                if (System.Diagnostics.Debugger.IsAttached)
                {
                    // An unhandled exception has occurred; break into the debugger
                    System.Diagnostics.Debugger.Break();
                }
            }
    
    
            /// <summary>
            /// Agent that runs a scheduled task
            /// </summary>
            /// <param name="task">
            /// The invoked task
            /// </param>
            /// <remarks>
            /// This method is called when a periodic or resource intensive task is invoked
            /// </remarks>
            protected override void OnInvoke(ScheduledTask task)
            {
                //TODO: Add code to perform your task in background
                IdentityManager.Current.ChallengeMethod = Challenge;
                
                
            }
    
            private void Challenge(string url,
                          IdentityManager.CredentialCompleteHandler callback,
                          IdentityManager.GenerateTokenOptions options)
            {
                // Pop-up dialog box to enter credentials - user action to call GenerateCredentialAsync. 
                IdentityManager.Current.GenerateCredentialAsync(url, "user1", "user1",
                    (credential, ex) =>
                    // lambda used to create anonymous block so call to GenerateCredentialAsync is self-contained.
                    {
                        callback(credential, ex);
                    },
                    options);                 
            }
    
            void QueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs args)
            {
                FeatureSet featureSet = args.FeatureSet;
    
                //Get Total Jobs in Que
                int JobCount = featureSet.Features.Count;
                //string LastJobType = featureSet.Features
    
                ShellTile PrimaryTile = ShellTile.ActiveTiles.First();
    
                if (PrimaryTile != null)
                {
                    StandardTileData tile = new StandardTileData();
    
                    //tile.BackgroundImage = new Uri("images/sadface.png", UriKind.Relative);
                    tile.Count = JobCount;
                    tile.Title = "added by background agent";
                    tile.BackTitle = "Get to work dude";
                    tile.BackContent = "Buddy";
                    PrimaryTile.Update(tile);
                }
    
                NotifyComplete();
            }
        }
    }

+ Reply to Thread

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts