I'm using CreateLayer in my Add-In and passing an auth token to create a layer from a REST Map Server.
The issue I'm having is that after the token times out, the layers will no longer work and they have to create the layers again through a ribbon button I provided.
Is there a way to force the token to be generated after a timeout? Maybe I can receive an event in the add-in??? I just have no idea what to search for to make this happen.
Thanks in advance!
Use the SDK’s Authentication/Identity manager (AuthenticationManager / IdentityManager / ArcGISIdentityManager depending on product & API) and register credentials with it. The SDK will then attach credentials to requests and will call a registered challenge/callback when a token is rejected so your code can obtain a fresh token (or the SDK will handle refresh if you used an OAuth flow that issues refresh tokens). This is the usual, robust pattern.
var serviceUri = new Uri("https://myserver.example.com/arcgis/rest/services");
string token = await GetAccessTokenFromYourServerAsync(); // your token fetch
var expires = DateTimeOffset.UtcNow.AddMinutes(30); // use the expiry the token endpoint returned
// PregeneratedTokenCredential / AccessTokenCredential or similar depending on SDK
var credential = new PregeneratedTokenCredential(token, serviceUri, expires);
AuthenticationManager.Current.AddCredential(credential);
Thanks for that. Is this relative to ArcGIS Pro SDK 3.3? I can't find anything like the TokenCredential classes.
Also, where would this code go if I could get it to work? Module Initialize?
Have you tried the out-of-the-box functionality CreateLayer?
You probably need to look into ArcGISPortal class for the Pro Addin or IArcGISSignOn for the CoreHost app for advanced token handling. Also, https://pro.arcgis.com/en/pro-app/3.3/help/projects/sign-in-to-your-organization.htm is worth looking at.
I use CreateLayer with passing a token to create the layers initially and don't see anything that can handle the token timeouts related directly to CreateLayer. I'll look at those links.