Select to view content in your preferred language

Accces a secured feature service using Proxy Configuration

993
0
07-29-2021 02:15 AM
MANESK
by
Occasional Contributor

Hi... I'm using an ESRI sample code for connecting a Secured Feature service using PROXY method.
My organization setup is Server federated with ArcGIS Portal.
I'm hosting the application from my local machine for testing purpose.

I tested using the mentioned URL below, and I'm getting response.
http://localhost/DotNet/proxy.ashx?https://services.arcgisonline.com/ArcGIS/rest/services/World_Topo...

When tested with
http://localhost/DotNet/proxy.ashx?https://mydomain/server/rest/services/Channels/FeatureServer/0?f=...
i receive the error.
{"error":{"code":499,"message":"Token Required", "details":[]}}

The user and password I'm using to access the secured service is created from portal with admin privileges.
I've attached the code, proxy configuration and the response from JS for your review and i'd like to know whether I'm missing something or committing any error in the code.

 

<script src="https://js.arcgis.com/3.37/"></script>
<script>
  var map, cred = "esri_jsapi_id_manager_data"; // cookie/local storage name

  require([
    "dojo/_base/unload",
    "dojo/cookie",
    "dojo/json",
    "dojo/parser", "esri/config", "esri/IdentityManager", "esri/layers/FeatureLayer",
    "esri/map",
    "dijit/layout/BorderContainer",
    "dijit/layout/ContentPane",
    "dojo/domReady!",
  ], function (baseUnload,
    cookie,
    JSON,
    parser, esriConfig, esriId, FeatureLayer,
    Map){

    // store credentials/serverInfos before the page unloads
    baseUnload.addOnUnload(storeCredentials);

    // look for credentials in local storage
    loadCredentials();

    parser.parse();
    esriConfig.defaults.io.proxyUrl = "http://localhost/DotNet/proxy.ashx";
    esriConfig.defaults.io.alwaysUseProxy = true;
    esri.config.defaults.io.corsEnabledServers.push("MYDOMAIN");
    map = new Map("mapCanvas", {
      basemap: "topo",
      center: [27.00, -6.00],
      zoom: 3
    });

    //add the secure service - token is required
    var secureLayer = new FeatureLayer("https://MYDOMAIN/server/rest/services/Channels/FeatureServer/0",
      {
        mode: FeatureLayer.MODE_ONDEMAND,
        outFields: ["*"]
      });
    map.addLayer(secureLayer);

    function loadCredentials(){
      var idJson, idObject;

      if (supports_local_storage()) {
        // read from local storage
        idJson = window.localStorage.getItem(cred);
      }
      else {
        // read from a cookie
        idJson = cookie(cred);
      }

      if (idJson && idJson != "null" && idJson.length > 4) {
        idObject = JSON.parse(idJson);
        esriId.initialize(idObject);
      }
      else {
        // console.log("didn't find anything to load :(");
      }
    }

    function storeCredentials(){
      // make sure there are some credentials to persist
      if (esriId.credentials.length === 0) {
        return;
      }

      // serialize the ID manager state to a string
      var idString = JSON.stringify(esriId.toJson());
      // store it client side
      if (supports_local_storage()) {
        // use local storage
        window.localStorage.setItem(cred, idString);
        // console.log("wrote to local storage");
      }
      else {
        // use a cookie
        cookie(cred, idString, {expires: 1});
        // console.log("wrote a cookie :-/");
      }
    }

    function supports_local_storage(){
      try {
        return "localStorage" in window && window["localStorage"] !== null;
      } catch (e) {
        return false;
      }
    }
  });
  </script>

 

0 Kudos
0 Replies