Passing Token to Service using ARCGIS JavaScript 4.0

4525
4
09-09-2016 08:02 AM
CraigMoran
New Contributor

In my application, I'm trying to add a FeatureLayer to a map.  The FeatureLayer url requires a token, which is created when a user name and password are provided.  I don't want the user to have to enter the user name and password, so what I've done is create a method that sends the user name and password to our token service, which then returns the token.  I then pass the token to the FeatureLayer as follows:

var map = new Map({
basemap: "streets",


slider: false
});

var view = new MapView({
container: "viewDiv",
center: [-77.007308, 38.875827],
zoom: 18,
map: map
});

var supportFeatureLayer = new FeatureLayer({

url: config.layers.support + "?token=" + GISToken,
token: GISToken,

outFields: ["*"]
});

 map.add(supportFeatureLayer);

However, I still get a "Sign In" prompt, despite that I see data being returned by the REST service, using IE's developer tools.  I've attached a screenshot.  Is this the correct way to pass a token to a REST service?  Is there no way to prevent the "Sign In" prompt returned by the service?

4 Replies
ReneRubalcava
Frequent Contributor

Ideally, you'd want to use OAuth or a Proxy to manage this for you.

If you really want to manage this, you can try using the IdentityManager.registerToken() method to register the token with the IdentityManager to it is available for future requests. Provide it with an object as specified and that should work.

You could also use the IdentityManager.generateToken() method to help you create the token.

EvelynHernandez
Occasional Contributor III

I do this:

  serviceURL + "layer/FeatureServer/1?f=json&token=" + token.read();

Where:

service url is ur rest service url and token.read() is a fx where i save the layer for future request.

The other way is doing it like odoe says but i havent tried.

I Hope it works for u

0 Kudos
CraigMoran
New Contributor

That's the thing, I can get the token and am already passing the token to the service like this:

var supportFeatureLayer = new FeatureLayer(OurRESTUrl + "?token=" + GISToken,{

outFields: ["*"]
});

I've already found a way to get the token back from our token service URL.  The problem is that even when I pass it to the FeatureLayer -- as described above -- I get a login screen.  When I look at the developer tools, I see a request being sent to our REST service URL (http://<REST_SERVICE_URL>?f=json&token=mE9rsU4yaSaGSnfgGas2o0… ), and I also see data being sent back like this:

{"currentVersion":10.31,"id":6,"name":"TOA.Supports","type":"Feature Layer","description":"","copyrightText":"","defaultVisibility":true,"editFieldsInfo":{"creationDateField":"created_date","creatorField":"created_user","editDateField":"last_edited_date","editorField":"last_edited_user"},"ownershipBasedAccessControlForFeatures":null,"syncCanReturnChanges":true,"relationships":[{"id":1,"name":"TOA.Signs_O","relatedTableId":1,"cardinality":"esriRelCardinalityOneToMany","role":"esriRelRoleOrigin","keyField":"GLOBALID","composite":true},{"id":0,"name":"TOA.Signs_D","relatedTableId":0,"cardinality":"esriRelCardinalityOneToMany","role":"esriRelRoleOrigin","keyField":"GLOBALID","composite":true},{"id":3,"name":"TOA.Signs_P","relatedTableId":2,"cardinality":"esriRelCardinalityOneToMany","role":"esriRelRoleOrigin","keyField":"GLOBALID","composite":true},{"id":4,"name":"TOA.Signs_R","relatedTableId":3,"cardinality":"esriRelCardinalityOneToMany","role":"esriRelRoleOrigin","keyField":"GLOBALID","composite":true},{"id":5,"name":"TOA.Signs_S","relatedTableId":4,"cardinality":"esriRelCardinalityOneToMany","role":"esriRelRoleOrigin","keyField":"GLOBALID","composite":true},{"id":6,"name":"TOA.Signs_W","relatedTableId":5,"cardinality":"esriRelCardinalityOneToMany","role":"esriRelRoleOrigin","keyField":"GLOBALID","composite":true},{"id":7,"name":"Meters","relatedTableId":9,"cardinality":"esriRelCardinalityOneToMany","role":"esriRelRoleOrigin"....

So, the service is sending data something back to my application, but I still get a login window.

I saw this post --Bypass IdentityManager Popup in ESRI ArcGIS JavaScript API | River on Bridge... -- but I after implementing the JavaScript code, I still get the login window.


0 Kudos
EvelynHernandez
Occasional Contributor III

Thats wrong, see how i put it in the example above.

And if u are adding more than one service, u need to do it for each one.

0 Kudos