Select to view content in your preferred language

Unable to use browser login when WPF app packaged

156
5
Jump to solution
2 weeks ago
ViktorSafar
Occasional Contributor II

I created a WPF app using the ArcGIS Maps SDK .NET WPF App (Esri) template.

I copied in the ArcGISLoginPrompt class from the WPF samples.

I created a new app in AGOL and updated the appId in the WPF app.

The app runs, I am presented with the AGOL login window, can log in, and the app presents the protected resource (basemap, as configured from the Visual Studio template).

 

I then added a Windows Application Packaging Project project and added the WPF app as a reference.

Running the app via the packaging project I get
Esri.ArcGISRuntime.Http.ArcGISWebException: 'Unable to generate token.'

from within 

credential = await AuthenticationManager.Current.GenerateCredentialAsync(info.ServiceUri);

 

If I switch Startup project back to the WPF app, it works fine again.

 

Using Fiddler I can see that when running via the packaging project, the app is calling /sharing/generateToken without any credentials so that error makes sense. The question is why is it calling that and not presenting the browser window.

 

Repro repo https://github.com/viktor-safar-geodata/wpf-arcgis-packaged

0 Kudos
1 Solution

Accepted Solutions
dotMorten_esri
Esri Notable Contributor

Sorry I should have been more clear. I wasn't suggesting to get rid of the persistence permanently - it was merely as a step to troubleshooting what is causing it. We now know that it is related to persistence.

This actually makes a lot of sense, because the entire purpose of the class is to avoid getting prompted for credentials, and fiddler confirms that it goes straight to generating a token for you based on the stored oauth token. My guess is you've already signed into the app in an earlier session, which is why you no longer are getting prompted with that line turned on.

Did you try adding a call to
           await AuthenticationManager.Current.RemoveAndRevokeAllCredentialsAsync();
and restart the app? That should clear any stored credentials, so on next app run you'll be prompted again.

Again I wasn't able to reproduce the issue with your application.

View solution in original post

0 Kudos
5 Replies
dotMorten_esri
Esri Notable Contributor

I see the same behavior for both. I wonder if it's because you got persistence enabled ?

Try removing this line: https://github.com/viktor-safar-geodata/wpf-arcgis-packaged/blob/main/WpfMapApp1/App.xaml.cs#L27

It has no effect on an unpackaged app, but it makes sure you don't need to sign in over and over again if you're using a packaged app. So if you already signed in earlier, it won't be asking you again until you actively sign out by calling

await AuthenticationManager.Current.RemoveAndRevokeAllCredentialsAsync();

0 Kudos
ViktorSafar
Occasional Contributor II

While deleting that line indeed fixed the problem, I am confused.

I wanted to persist the credentials, so I added in default persistence by calling that line 27.

But for WPF apps the default persistence works only if the app is packaged (ref Matvei talking about it from around 43:50 https://mediaspace.esri.com/media/t/1_f10emt89 ) so I added a packaging project.

Without the call to CredentialPersistence.CreateDefaultAsync there is no persistence so how do I make these two to work together?

 

Side note: I have implemented a custom credential persistence and ditched the packaging project, but I would still like to know how it's supposed to work with the default persistence and the packaging project.

0 Kudos
dotMorten_esri
Esri Notable Contributor

Sorry I should have been more clear. I wasn't suggesting to get rid of the persistence permanently - it was merely as a step to troubleshooting what is causing it. We now know that it is related to persistence.

This actually makes a lot of sense, because the entire purpose of the class is to avoid getting prompted for credentials, and fiddler confirms that it goes straight to generating a token for you based on the stored oauth token. My guess is you've already signed into the app in an earlier session, which is why you no longer are getting prompted with that line turned on.

Did you try adding a call to
           await AuthenticationManager.Current.RemoveAndRevokeAllCredentialsAsync();
and restart the app? That should clear any stored credentials, so on next app run you'll be prompted again.

Again I wasn't able to reproduce the issue with your application.

0 Kudos
ViktorSafar
Occasional Contributor II

I see your point and you were correct. Upon inspection of `AuthenticationManager.Current.Persistence` loaded from CredentialPersistence.CreateDefaultAsync() there was an outdated credential.

Interestingly, RemoveAndRevokeAllCredentialsAsync() threw

'Some credentials could not be revoked (RevokeTokenUrl cannot be null or empty.)'

ViktorSafar_0-1722495488931.png

 

I could not figure out how to remove it, so I gave 

CredentialPersistence.CreateDefaultAsync()

a parameter

CredentialPersistence.CreateDefaultAsync('vik-dev')

to create a new persistence and then it all started working.

 
0 Kudos
dotMorten_esri
Esri Notable Contributor

You can most likely safely catch and ignore any errors from `RemoveAndRevokeAllCredentialsAsync`.
It makes some requests to online to just ensure the token is completely revoked, and that request could potentially fail.
Uninstalling the app should also clear the credential cache (one of the benefits you get with the app packaging and why it is required)

0 Kudos