How do I Pass Token to Secure Service using ARCGIS JavaScript 4.x

1521
5
05-05-2020 10:09 AM
CristianNovoa
New Contributor

Hi all, I'm trying to pass a token to a secure FeatureLayer like this:

var clientLayer = new FeatureLayer({
    url: "https://xxxxxxx/MapServer/0?token="+token,
    outFields: ["*"]
  });

Then I Get :

I also using these methods to get the token:

var def = esriId.generateToken(serverInfo, {
        "username": usuario,
        "password": pass,
        "client""referer",
        "referer": document.URL
      });
      def.then(function (value) {
        
        token = value.token;
        
        esriId.registerToken({
          server : server,
          token : token,
          expires : 90
        });

So, How is the correct way to pass the token to secure layer?

Thanks

0 Kudos
5 Replies
ReneRubalcava
Frequent Contributor

You shouldn't need to pass the token manually.

If you create the ServerInfo to reference the server of your service, use generateToken, and then registerToken with the IdentityManager, and the API will automatically pass the token to the service with each request.

LeeIrminger
New Contributor II

Could someone please supply some sample code of what that process would look like please?


MiriEshel1
New Contributor II

Hello  Lee,

Have you got some sample code of what that process would look like? I need the same thing now....

Thanks,

Miri

0 Kudos
SafaaMamdouh
New Contributor

This code sample is useful

https://codepen.io/benesri/pen/ExxwqMB

0 Kudos
MiriEshel1
New Contributor II

Hello @SafaaMamdouh ,

Thank you so much for your code. It is clear and easy to understand.

The thing is, that it asks the user for credentials (which I prefer not to) or if it is written hard coded in the code, it is very easy to find (you just open F12 and see it).

I'm looking for 2 options, depend on the identity store:

a. If it is a BUILT-IN identity store, I'm looking for a way to dedicate a user that will have the permissions to view the service and kind of a web service (in server side) that will get this user name and password and create a token based on this user & password, something like the proxy.ashx that was supported in the past. Now I understand that it is not recommended/supported anymore.

b. If it is WINDOWS identity store, I'm looking for a way to fetch the user name & password of the user and generate the token accordingly.

I hope I explained myself well....

Thanks again,

Miri

0 Kudos