<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Accces a secured feature service using Proxy Configuration in ArcGIS JavaScript Maps SDK Questions</title>
    <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/accces-a-secured-feature-service-using-proxy/m-p/1083861#M74063</link>
    <description>&lt;P&gt;Hi... I'm using an ESRI sample code for connecting a Secured Feature service using PROXY method.&lt;BR /&gt;My organization setup is Server federated with ArcGIS Portal.&lt;BR /&gt;I'm hosting the application from my local machine for testing purpose.&lt;/P&gt;&lt;P&gt;I tested using the mentioned URL below, and I'm getting response.&lt;BR /&gt;&lt;STRONG&gt;&lt;A href="http://localhost/DotNet/proxy.ashx?https://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer?f=json" target="_blank" rel="noopener"&gt;http://localhost/DotNet/proxy.ashx?https://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer?f=json&lt;/A&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;When tested with&lt;BR /&gt;&lt;STRONG&gt;&lt;A href="http://localhost/DotNet/proxy.ashx?https://mydomain/server/rest/services/Channels/FeatureServer/0?f=json" target="_blank" rel="noopener"&gt;http://localhost/DotNet/proxy.ashx?https://mydomain/server/rest/services/Channels/FeatureServer/0?f=json&lt;/A&gt;&lt;/STRONG&gt;&lt;BR /&gt;i receive the error.&lt;BR /&gt;&lt;STRONG&gt;{"error":{"code":499,"message":"Token Required", "details":[]}}&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;The user and password I'm using to access the secured service is created from portal with admin privileges.&lt;BR /&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;&amp;lt;script src="https://js.arcgis.com/3.37/"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script&amp;gt;
  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 &amp;amp;&amp;amp; idJson != "null" &amp;amp;&amp;amp; idJson.length &amp;gt; 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 &amp;amp;&amp;amp; window["localStorage"] !== null;
      } catch (e) {
        return false;
      }
    }
  });
  &amp;lt;/script&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 29 Jul 2021 09:15:49 GMT</pubDate>
    <dc:creator>MANESK</dc:creator>
    <dc:date>2021-07-29T09:15:49Z</dc:date>
    <item>
      <title>Accces a secured feature service using Proxy Configuration</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/accces-a-secured-feature-service-using-proxy/m-p/1083861#M74063</link>
      <description>&lt;P&gt;Hi... I'm using an ESRI sample code for connecting a Secured Feature service using PROXY method.&lt;BR /&gt;My organization setup is Server federated with ArcGIS Portal.&lt;BR /&gt;I'm hosting the application from my local machine for testing purpose.&lt;/P&gt;&lt;P&gt;I tested using the mentioned URL below, and I'm getting response.&lt;BR /&gt;&lt;STRONG&gt;&lt;A href="http://localhost/DotNet/proxy.ashx?https://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer?f=json" target="_blank" rel="noopener"&gt;http://localhost/DotNet/proxy.ashx?https://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer?f=json&lt;/A&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;When tested with&lt;BR /&gt;&lt;STRONG&gt;&lt;A href="http://localhost/DotNet/proxy.ashx?https://mydomain/server/rest/services/Channels/FeatureServer/0?f=json" target="_blank" rel="noopener"&gt;http://localhost/DotNet/proxy.ashx?https://mydomain/server/rest/services/Channels/FeatureServer/0?f=json&lt;/A&gt;&lt;/STRONG&gt;&lt;BR /&gt;i receive the error.&lt;BR /&gt;&lt;STRONG&gt;{"error":{"code":499,"message":"Token Required", "details":[]}}&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;The user and password I'm using to access the secured service is created from portal with admin privileges.&lt;BR /&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;&amp;lt;script src="https://js.arcgis.com/3.37/"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script&amp;gt;
  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 &amp;amp;&amp;amp; idJson != "null" &amp;amp;&amp;amp; idJson.length &amp;gt; 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 &amp;amp;&amp;amp; window["localStorage"] !== null;
      } catch (e) {
        return false;
      }
    }
  });
  &amp;lt;/script&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Jul 2021 09:15:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/accces-a-secured-feature-service-using-proxy/m-p/1083861#M74063</guid>
      <dc:creator>MANESK</dc:creator>
      <dc:date>2021-07-29T09:15:49Z</dc:date>
    </item>
  </channel>
</rss>

