MAUI Authentication Issues

114
3
Jump to solution
2 weeks ago
Labels (1)
JoeHershman
MVP Regular Contributor

I am migrating an app from Xamarin to Maui, currently focused on iOS.

I have the Authentication setup and things appear to work, however, when trying to query a service table I get what seems to be authentication issues.  I even added an extra authentication specific to the service and while I get a valid credential and add to AuthenticationManager I still get an apparent authentication error.

Error:

"You do not have permissions to access this resource or perform this operation."
Source: "System.Private.CoreLib"
   at Esri.ArcGISRuntime.Http.HttpDispatcher.SendAsync(HttpRequestMessage request, HandlerOptions options, CancellationToken cancellationToken)
   at Esri.ArcGISRuntime.Internal.RequestHandler.IssueRequestAndRespond(CoreRequest request)

 Code

//I am already authenticated to AGOL, but added this for testing.  It does fire an OAuth window
//The credential is valid, I can even use the token from debugger to access service
var credential = await AuthenticationManager.Current.GenerateCredentialAsync(new Uri(_configuration.AppSettings.ProjectFeatureLayerUrl));
AuthenticationManager.Current.AddCredential(credential);

var table = new ServiceFeatureTable(new Uri(_configuration.AppSettings.ProjectFeatureLayerUrl));
await table.LoadAsync();  //THIS LINE FAILS, was added for testing

var parameters = new QueryParameters { WhereClause = $"{_configuration.AppSettings.FieldNames.TrackingStatus} ='Project Created'", ReturnGeometry = true, MaxFeatures = 1000 };

var results = await table.QueryFeaturesAsync(parameters, QueryFeatureFields.LoadAll);  //If does have the LoadAsync this fails

As noted:  In the debugger I can grab the token and use in a browser to access that service, so it seems I am getting a valid credential.  It seems though, that the AuthenticationManager is not being queried for credentials.

Thanks

-Joe

Thanks,
-Joe
0 Kudos
1 Solution

Accepted Solutions
JoeHershman
MVP Regular Contributor

It seem the error is because in my Maui startup I was setting a default API key

.UseArcGISRuntime(config => config.UseApiKey("API KEY"));

Replaced with:

.UseArcGISRuntime();

 And now it works.  Not sure why, but remove the default key has resolved the issue

Thanks,
-Joe

View solution in original post

0 Kudos
3 Replies
JoeHershman
MVP Regular Contributor

After some additional research I found a MAUI sign in sample MauiSignin.  This includes setting  Persistence property on AuthenticationManager

AuthenticationManager.Current.Persistence = await CredentialPersistence.CreateDefaultAsync();

Unfortunately, this did not resolve the issue. 

I still fail trying to query the service layer even though it would seem that everything is set correct and I have permissions

Thanks,
-Joe
0 Kudos
JoeHershman
MVP Regular Contributor

It seem the error is because in my Maui startup I was setting a default API key

.UseArcGISRuntime(config => config.UseApiKey("API KEY"));

Replaced with:

.UseArcGISRuntime();

 And now it works.  Not sure why, but remove the default key has resolved the issue

Thanks,
-Joe
0 Kudos
Nicholas-Furness
Esri Regular Contributor

Not sure why, but remove the default key has resolved the issue

If you set a default API Key, it will be used for all requests so even if you configure OAuth, the OAuth/Authentication Manager workflows won't be kicked off.

By no longer setting it, the SDK will interrogate services and any auth config you've set up and use that info to manage authentication. If you need API Keys for specific services, you can still set them directly on those SDK objects (just not globally) and use a mix of API Key and OAuth authentication.