How to provide subscription credentials via .Net SDK?

682
0
10-03-2016 12:55 PM
TomSmith2
New Contributor

We recently started using the ArcGIS .Net Runtime 10.2.7 in our web app, specifically to use the reversegeocodeasync operation.  We managed to get everything working correctly but now we are trying to update our logic to include the ArcGIS subscription credentials in the source code so our ArcGIS subscription is used in our reverse geocode requests.  Our users won't be entering any usernames or passwords so it appears that we would need to use the app authentication based on the documentation we have found on the ArcGIS website.

However, after reading through the documentation we still can't figure out how to do this.  So far we have:

  1. Set the ClientID prior to initialization.  (ArcGISRuntimeEnvironment.ClientID)
  2. Initialized the ArcGISRuntimeEnvironment.

At this point we can successfully make requests via the ReverseGeocodeAsync request, however using Fiddler and examining the GET request we don't see the clientID anywhere which leads us to believe our account still isn't being used for the request.  We also aren't seeing any usage in the usage history of our account which also makes it look like we are still missing something in order for our account to be used for the request.  This led to us trying numerous ways of generating tokens for token authentication and trying to use the OAuth authentication by supplying the ClientId and ClientSecret.  However, these attempts have all failed with exceptions ranging from "Challenge Handler not being provided" to "Authorization URL not provided" and other exceptions.  When we try to research the exceptions it ends up looking like we are on the wrong track and we're hoping we can get some feedback here to help set us straight.

Could anyone tell us how we should provide our ClientId and/or ClientSecret in our request so that the request is counted against our ArcGIS subscription?  Or if anyone could provide any example code that would be great too.

To summarize, we only need to make a ReverseGeocodeAsync request, our users will not be providing any usernames or passwords (we want to use our app credentials such as ClientId and/or ClientSecret), and we're using the .Net SDK 10.2.7 in our web app project.  We also won't be doing any local geocoding, we are just using the results of the ReverseGeocodeAsync request to display the address to the user.

Sample Source Code Below (sorry for formatting, not sure how to make it display more clearly):

public static async Task<LocatorReverseGeocodeResult> GetAddress(double latitude, double longitude)
{

if (!Esri.ArcGISRuntime.ArcGISRuntimeEnvironment.IsInitialized)
{
Esri.ArcGISRuntime.ArcGISRuntimeEnvironment.ClientId = System.Configuration.ConfigurationManager.AppSettings["ArcGISClientID"];
try
{
Esri.ArcGISRuntime.ArcGISRuntimeEnvironment.Initialize();
}
catch (Exception ex)
{
Trace.WriteLine("Unable to initialize the ArcGIS Runtime with the client ID provided. Error Details: " + ex.Message);
}
}

Uri uri = new Uri("http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer");

SpatialReference spatialRef = new SpatialReference(102100);
LocatorTask _locator = new OnlineLocatorTask(uri);
MapPoint geoPoint = new MapPoint(latitude, longitude, spatialRef);
System.Threading.CancellationTokenSource canceller = new System.Threading.CancellationTokenSource();
canceller.CancelAfter(30000);
System.Threading.CancellationToken cancelToken = canceller.Token;
LocatorReverseGeocodeResult addressInfo = null;
double distance = 50;
try
{
System.Threading.Tasks.Task<LocatorReverseGeocodeResult> geocodeTask;
geocodeTask = _locator.ReverseGeocodeAsync(geoPoint, distance, spatialRef, cancelToken);

geocodeTask.Wait(30000);
addressInfo = geocodeTask.Result;
}
catch (System.Threading.Tasks.TaskCanceledException cancelException)
{
//Handle cancel exception
Trace.WriteLine("cancelException: " + cancelException.Message);
}
catch (Esri.ArcGISRuntime.Http.ArcGISWebException arcGisException)
{
//Handle ArcGis web exception
Trace.WriteLine("arcGisException: " + arcGisException.Message);
}
catch (Exception generalException)
{
//Handle General exception
Trace.WriteLine("generalException: " + generalException.Message);
}
return addressInfo;

}

Thank you.

0 Kudos
0 Replies