En la documentación de ArcGIS Runtime SDK for .NET (versión de SDK 100.3.0), se encuentran ejemplos sobre autenticación y sobre consultas a un Feature Layer público, sin embargo, no encontré un ejemplo sobre una consulta a un Feature Layer privado (requiere autenticación), lo que me tomó algunas horas realizar.
Comparto el ejemplo realizado (app de consola):
Program.cs
Comparto el ejemplo realizado (app de consola):
Program.cs
- using System;
- using Esri.ArcGISRuntime.Data;
- using System.Collections.Generic;
- namespace arcgisAuthTest
- {
- class Program
- {
- static void Main(string[] args)
- {
- string serviceUrl = "https://services6.arcgis.com/FFF6MrlVn5PAw2F8/arcgis/rest/services/service_bf963161c24047259028034xxxxxx/FeatureServer/0";
- // Would be better to read them from a secure source
- string username = "usr";
- string password = "pa$$";
- var i = 0;
- string query = "upper(placa_motocicleta) LIKE '%342FFF%'";
- "*"
- };
- var listado = flq.QueryFeature(query, outFields);
- foreach (Feature f in listado)
- {
- Console.WriteLine(string.Format(Environment.NewLine "Feature {0}", i++));
- for (int j = 0; j < f.FeatureTable.Fields.Count; j++)
- {
- string fieldName = f.FeatureTable.Fields[j].Name;
- Console.WriteLine(string.Format("f[{0}] = '{1}'", fieldName, f.Attributes[fieldName]));
- }
- }
- Console.ReadKey();
- }
- }
- }
- using Esri.ArcGISRuntime.Data;
- using Esri.ArcGISRuntime.Security;
- using System;
- using System.Collections.Generic;
- using System.Threading.Tasks;
- namespace arcgisAuthTest
- {
- /// <summary>
- /// Ejecución de queries sobre un Feature Layer de Arcgis Online.
- /// Incluye autenticación por medio de usuario y contraseña.
- /// Author: Cristian de León R.
- /// </summary>
- public class FeatureLayerQuery
- {
- private string _password;
- private Credential _credential;
- public string ServiceUrl { get; private set; }
- public string Usuario { get; private set; }
- public ServiceFeatureTable SrvFeatureTable { get; private set; }
- public FeatureLayerQuery(string serviceUrl, string usuario, string password)
- {
- Usuario = usuario;
- _password = password;
- ServiceUrl = serviceUrl;
- {
- AuthenticationType = AuthenticationType.Token,
- }).GetAwaiter().GetResult();
- }
- /// <summary>
- /// Ejecutar una consulta contra un Feature Layer de Arcgis Online.
- /// </summary>
- /// <param name="whereClause">Ej.: "upper(placa_motocicleta) LIKE '%" + (NumPlaca.Trim().ToUpper()) + "%'"</param>
- /// <param name="outFields">listado de campos a retornar o * para devolver todos</param>
- /// <param name="maxFeatures">Número máximo de features devuelto</param>
- /// <returns></returns>
- public List<Feature> QueryFeature(string whereClause, List<string> outFields, int maxFeatures = 0)
- {
- SrvFeatureTable.Credential = _credential;
- // Create a query parameters that will be used to Query the feature table
- {
- WhereClause = whereClause,
- MaxFeatures = maxFeatures
- };
- // Query the feature table
- FeatureQueryResult queryResult = SrvFeatureTable.PopulateFromServiceAsync(queryParams, true, outFields).GetAwaiter().GetResult();
- foreach (Feature r in queryResult)
- {
- features.Add(r);
- //Console.WriteLine(r.Attributes["placa_motocicleta"]);
- }
- return features;
- }
- /// <summary>
- /// Challenge method that checks for service access with known (hard coded) credentials
- /// </summary>
- /// <param name="info"></param>
- /// <returns></returns>
- private async Task<Credential> CreateKnownCredentials(CredentialRequestInfo info)
- {
- Credential knownCredential = null;
- if (info.ServiceUri.AbsoluteUri.ToLower().Contains("arcgis.com"))
- {
- knownCredential = await AuthenticationManager.Current.GenerateCredentialAsync(
- info.ServiceUri,
- Usuario,
- _password,
- info.GenerateTokenOptions);
- }
- return knownCredential;
- }
- }
- }