Select to view content in your preferred language

Help with OAuth

3004
30
Jump to solution
10-04-2023 11:30 AM
BrianBulla1
Occasional Contributor

Hi,

I'm just starting with the Maps SDK today, so sorry in advance for the stupid questions.  🙂

So I'm trying to do something that I think should be super simple, but I cannot for the life of me get it to work.  I'm using the DisplayAMap sample, changing the portal item to one under my AGOL account, and trying to display it.  The missing componenet is the OAuth, since I'll need to enter a username/password to get to the webmap.

I'm looking at the "Secutrity" samples in the .NET WPF app, but don't quite understand how to integrate it into what I already have working in the DisplayAMap Sample.

There seems to be this "ArcGISLoginPrompt.cs" file, that doesn't display the code for in the app.  I'm guessing that's an important part of it??

Sorry for the cryptic question....I just have no clue what to even ask.

0 Kudos
30 Replies
BrianBulla1
Occasional Contributor

Sorry...had to delete a post....still not working.

0 Kudos
BrianBulla1
Occasional Contributor

Thanks, but that didn't really maike any difference.  Still just a blank screen.  Seems like the only webmap I can get to work with login credentials is the esri sample one.

I have a regular AGOL account as well as an Organizational account, and trying to load up a webmap from either one where I would need to enter credentials just doesn't work.  Webmaps shared as "Everyone" seem to work fine.

0 Kudos
JoeHershman
MVP Regular Contributor

If you created an OAuth key through your developer account this would only be attached to your developer account.  Meaning it will only give you access to items stored on your developer AGOL.  An organizational account would require you to create a OAuth App in that Organization.  Have you tried to create a map in the AGOL site that is accessed by your developer login (I assume the BrianBulla1 named user) that you use to access the developer dashboard?

I have a developer site that came with my dev account, although I don't use.  But my company has an Organizational account.  An map that is setup in my Organization could not be accessed with an OAuth app I setup on the developer dashboard because it is a different account,  I could access the traffic layer with my dashboard OAuth

Thanks,
-Joe
0 Kudos
BrianBulla1
Occasional Contributor

Hi,

Yes, my testing has mainly be with useing the same dev. account and AGOL webmap.

Not quite sure how it's all setup, but with the esri samples, you need to add your own API key and OAuth details.  Those are both in my developer account, but in the samples all of the webmaps etc. that you access are esri resources, and everything seems to work using those ID's.

There must be something I'm missing....I just have no clue what it would be.

 

0 Kudos
ThadTilton
Esri Contributor

If you successfully log in with OAuth, you don't need to provide an API key.

If it works for the map used in the sample but not for your map, perhaps the account you're logging in with does not have the expected access to that map. If you log in to ArcGIS Online with the same creds, can you access the web map?

0 Kudos
BrianBulla1
Occasional Contributor

Hi @ThadTilton .  As far as I'm aware, with the esri samples if you do not provide an API key they will not work.  There is a specific line of code/instructions that tells you to "insert API Key here".

As for my login, I can definitely see the webmap when I login online.  The map will also popup in my test app if I set the access level to "Everyone".  It only doesn't work if I set the access level to "Private".....which should generate the username/password screen, but instead the app just hangs there.

What's also weird is if I set my map to "Everyone" so that map actually loads, and then (in my code) add a layer that also needs authentication (esri World Traffic Service....just like in the sample), then I do get prompted for the username/password.

It only seems to hang when my own webmap needs authentication.  So this works when the webmap on line #12 is set to "Everyone", but as soon as I turn it back to "Private" everything hangs.

        private async Task SetupMap()
        {

            // Add the ArcGIS Online URL to the authentication helper.
            AuthenticationHelper.RegisterSecureServer("https://www.arcgis.com/sharing/rest");

            // Create a portal. If a URI is not specified, www.arcgis.com is used by default.
            ArcGISPortal portal = await ArcGISPortal.CreateAsync();

            // Get the portal item for a web map using its unique item id.
            
            PortalItem mapItem = await PortalItem.CreateAsync(portal, "4f7b45cd7d034ea9a09ec58658bff70d");      //this is the ID of my AGOL webmap.  When set to "Everyone" it will load.

            // Create the map from the item.
            Map map = new Map(mapItem);

            // Create a layer to display the ArcGIS World Traffic service.
            var trafficServiceUrl = "https://traffic.arcgis.com/arcgis/rest/services/World/Traffic/MapServer";
            var trafficLayer = new ArcGISMapImageLayer(new Uri(trafficServiceUrl));

            // Handle changes in the traffic layer's load status to check for errors.
            trafficLayer.LoadStatusChanged += TrafficLayer_LoadStatusChanged;

            // Add the traffic layer to the map's data layer collection.
            map.OperationalLayers.Add(trafficLayer);

            // To display the map, set the MapViewModel.Map property, which is bound to the map view.
            this.Map = map;

        }

 

0 Kudos
JoeHershman
MVP Regular Contributor

This does not register your OAuth app

AuthenticationHelper.RegisterSecureServer("https://www.arcgis.com/sharing/rest");

You need to register your OAuth.  The OAuthClientInfo is required

		ServerInfo serverInfo = new ServerInfo(serverUri)
		{
			TokenAuthenticationType = TokenAuthenticationType.OAuthAuthorizationCode,
			OAuthClientInfo = new OAuthClientInfo(AppClientId, redirectUri)
			{
				UserInterfaceStyle = UserInterfaceStyle.Dark,
				ClientSecret = ClientSecret,
			},

		};

 

Thanks,
-Joe
0 Kudos
BrianBulla1
Occasional Contributor

Hi @JoeHershman 

As per the sample, here is what .RegisterSecureServer is doing (part of the AuthenticationHelper.cs part of the project....not posted above).  The clientID and redirect Url are set earlier in the code and match what I have setup in the Developer dashboard.

public static void RegisterSecureServer(string url, 
                                        string clientID = OAuthClientID, 
                                        string redirectUrl = OAuthRedirectUrl)
{
    // Define the server URL, authentication type, client ID, and redirect URL.
    ServerInfo portalServerInfo = new ServerInfo(new Uri(url))
    {
        TokenAuthenticationType = TokenAuthenticationType.OAuthAuthorizationCode,
        OAuthClientInfo = new OAuthClientInfo(clientID, new Uri(redirectUrl))
    };

    // Register the server information with the AuthenticationManager.
    AuthenticationManager.Current.RegisterServer(portalServerInfo);
}

 

This is more or less, what you suggested, but currently not working.  Not exactly sure how the redirect URL works, but I created exactly as the sample tutorial explained.

 

0 Kudos
PierreMasson
Occasional Contributor

I have exactly the same problem. I modified the sample to use a Feature Layer posted in our ArcGIS Online Organization. And everything works well. The login prompt appears when it hits that line:

PortalItem portalItem = await PortalItem.CreateAsync(arcgisPortal, WebMapId);

Then I created my own app. .Net 6, I do the register key, the ArcGISLoginPrompt.SetChallengeHandler (I copied the whole class in my test project).. When I hit the same line, the login prompt does not appear but instead I get:

Esri.ArcGISRuntime.Http.ArcGISWebException: 'You do not have permissions to access this resource or perform this operation.'

The only thing different I can see are the projects that runs that code. Like in mine I can't add the nuget package Esri.ArcGISRuntime.LocalServices. I don't know if it`s necessary. 

I'm really looking forward to an explanation to this. I spent all day compararing ans searching a solution to that..

0 Kudos
JoeHershman
MVP Regular Contributor

Instead of having the app open it when it gets the PortalItem can you add

 

//THIS LINE SHOULD OPEN WINDOW!
var credential = await AuthenticationManager.Current.GenerateCredentialAsync(sharingUri);

AuthenticationManager.Current.AddCredential(credential);

 

before

 

portal = await ArcGISPortal.CreateAsync(sharingUri);

 

 In your current code, if you put a breakpoint in the ChallangeHandler delegate does it get hit when you call (before making the change I describe above)

 

PortalItem mapItem = await PortalItem.CreateAsync(portal, "29ad2fc9b8794fb0b6dc1ca02948613d");    //This is the ID of my webmap.

 

 

Thanks,
-Joe
0 Kudos