How to log in with token srv

4582
5
05-09-2015 11:47 AM
EvelynHernandez
Occasional Contributor III

Hello,

Im trying to find some code to add to my app for logging in with tokens using a userid and pass for default but i dont know how to do it.

How can i do that using my code?

I have the following:

var map;


var dominio = "vialactea";
        var sl = new String('\\');
        var usuario = "ehernanr";
        var userid = dominio + sl + usuario;
        var pass = 'chilquinta5';
       
    require([
      "dojo/dom-construct",
      "esri/Color",
      "esri/dijit/Geocoder",
      "esri/dijit/Popup",
      "esri/InfoTemplate",
      "esri/layers/ArcGISDynamicMapServiceLayer",
      "esri/map",
      "esri/symbols/SimpleFillSymbol",
      "esri/symbols/SimpleLineSymbol", "dojo/domReady!",
      "esri/IdentityManager", 
     "esri/ServerInfo", 
      "esri/Credential", 
     "esri/request"
    ], function(
      domConstruct, Color, Geocoder, Popup, InfoTemplate,
      ArcGISDynamicMapServiceLayer, Map, SimpleFillSymbol, SimpleLineSymbol, idManager, ServerInfo, Credential, esriRequest
    ) {
      var sls = new SimpleLineSymbol("solid", new Color("#444444"), 3);
      var sfs = new SimpleFillSymbol("solid", sls, new Color([68, 68, 68, 0.25]));


      var popup = new Popup({
        fillSymbol: sfs,
        lineSymbol: null,
        markerSymbol: null
      }, domConstruct.create("div"));
     
     


      map = new Map("map", {
        basemap: "topo",
        center: [-71.0659,-32.9252], // long, lat
        zoom: 8,
        sliderStyle: "small",
        infoWindow: popup
      });
     
     
  
       //AÑADIR MAPSERVER
       var chqMapServer = new ArcGISDynamicMapServiceLayer(".../PMS/Concesiones/MapServer", { 
        "id": "concesiones", 
        "opacity": 0.75 
        });
       
        //TEMPLATE PARA MOSTRAR INFOWINDOW SSEE (0)
     var trans = new InfoTemplate();
        trans.setTitle("<b>ID: ${concesion_id}</b>");
      
        var transInfoContent =
        "<div style=padding-top: 10px;>Clase: ${clase}<br></div>" +
        "<div style=display:inline-block;width:8px;></div>";
       
        trans.setContent(transInfoContent);
       
         //TEMPLATE PARA MOSTRAR INFOWINDOW SSEE (1)
     var dist = new InfoTemplate();
        dist.setTitle("<b>ID: ${concesion_id}</b>");
      
        var distInfoContent =
        "<div style=padding-top: 10px;>Clase: ${clase}<br></div>" +
        "<div style=display:inline-block;width:8px;></div>";
       
        dist.setContent(distInfoContent);
       
        //Añadiendo Templates al MapServer
         chqMapServer.setInfoTemplates({ 
         0:{  infoTemplate: trans},
         1:{  infoTemplate: dist }
         }); 
       
         //Agregando la capa al mapa
  map.addLayer(chqMapServer);

  
  var chqMapServer2 = new ArcGISDynamicMapServiceLayer("...PMS/Electricidad/MapServer", { 
        "id": "electricidad", 
        "opacity": 0.75 
        });
       
         //TEMPLATE PARA MOSTRAR INFOWINDOW SSEE (1)
     var dist = new InfoTemplate();
        dist.setTitle("<b>ID: ${id_tramo}</b>");
      
        var distInfoContent =
        "<div style=padding-top: 10px;>Sed: ${sed}<br></div>" +
        "<div style=display:inline-block;width:8px;></div>";
       
        dist.setContent(distInfoContent);
       
  //Añadiendo Templates al MapServer
         chqMapServer2.setInfoTemplates({ 
         2:{ infoTemplate: dist}
         });         
  map.addLayer(chqMapServer2);
    
});

Thanks for all ur help as always !

0 Kudos
5 Replies
RobertScheitlin__GISP
MVP Emeritus

Evelyn,

     Token in the JS API are handled though the use of a Proxy.

0 Kudos
EvelynHernandez
Occasional Contributor III

Well i did with a code example using esri leaflet, but the thing is how can i integrate both codes.

The other one that i showed before and this one:

<script>
  var map = new L.Map('map').setView([34.326, -117.697], 12);
        var map;
        var dominio = "mydomain";
        var sl = new String('\\');
        var usuario = "myuser";
        var userid = dominio + sl + usuario;
       
  L.esri.basemapLayer('Topographic').addTo(map);


  function serverAuth(callback){
    L.esri.post('.../arcgis/tokens/generateToken', {
      username: userid,
      password: 'mypass',
      f: 'json',
      expiration: 86400,
      client: 'referer',
      referer: window.location.origin
    }, callback);
  }


  serverAuth(function(error, response){
    var dl = L.esri.dynamicMapLayer(...'arcgis/rest/services/MapaBase/MapServer', {
      opacity: 1,
      token:  response.token
    }).addTo(map);


    dl.on('authenticationrequired', function (e) {
      serverAuth(function(error, response){
        e.authenticate(response.token);
      });
    });
  });
</script>
0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Evelyn,

   As I mentioned before this is not the way to handle tokens in the JS API. But if you want to manually generate token and maintain there life cycle and recreate then manually when they expire then you can do the same as the L.esri.post using esriRequest. But be aware that the JS API does not provide a token property on ArcGISDynamicMapServiceLayer so if you manually add the token to the url for your layer then you will run into the same problem as outlined here:

How to avoid identity manager

I am just trying to save you a lot of headache, by telling you "use a proxy!"

EvelynHernandez
Occasional Contributor III

I see. The thing is now how to use a proxy... .

Is there any example about how to use one ? Im trying to find any guidance but no results

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Evelyn,

   Yes, on the JS API guide page there is a link.

Using the proxy | Guide | ArcGIS API for JavaScript