Select to view content in your preferred language

How do you reference the proxy.ashx page?

19365
25
08-09-2011 09:26 AM
MatthewGerbrandt
New Contributor II
Hello there. I've got some secured ArcGIS Services that require a token.  I've got my proxy.ashx and proxy.config pages in place and configured.

The last thing I need to do is figure out how to get my JS/HTML pages to reference those proxy pages and pass the token to the services.

If someone could please post some code or point me to an example, I'd be tremendously grateful.  The info at http://resources.esri.com/help/9.3/arcgisserver/apis/javascript/arcgis/help/jshelp_start.htm#jshelp/... wasn't helpful because my pages don't have a code-behind page for the C# code.

Thanks a million in advance!!
0 Kudos
25 Replies
HaroldBostic
Occasional Contributor II
Hello, if you are using a proxy page and the JSAPI .  In your JS code, sometime prior to making a request from a service you would set esri.config.defaults.io.proxyUrl  to the url of your ashx page.

All the examples from your link are for use inside your JS code,  They just demonstrate how you would call your proxy page based on your server side implementation

In side your ashx page would reside the secured urls and the token generated from the getToken rest endpoint that can access those service(s), no need to pass the token to your services as the proxy will do the authentication on your behalf
0 Kudos
MatthewGerbrandt
New Contributor II
Thank you very much for your answer.  I've added the following code to my web page:

esri.config.defaults.io.proxyUrl = "proxy.ashx";  // that is the correct path

And I'm getting the following error:


Webpage error details

User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C; .NET4.0E)
Timestamp: Mon, 15 Aug 2011 15:32:56 UTC


Message: 'esri.config.defaults' is null or not an object


Looks like I need to add a reference somewhere.  Can anyone point me in the right direction?
0 Kudos
HaroldBostic
Occasional Contributor II
Hello, an example is here:

http://help.arcgis.com/en/webapi/javascript/arcgis/help/jssamples/routetask_directions.html

is your call inside a dojo.addOnLoad call like dojo.addOnLoad(init);?  If not the esri namespace may not be loaded yet and it could be a timing issue.

Hope that helps.
0 Kudos
MatthewGerbrandt
New Contributor II
Thank you very much, Harold.  I think I'm closer to getting there but my code still isn't retrieving a token from the proxy page.

Here's the JavaScript code:

<script type="text/javascript">
    dojo.require("esri.map");
    dojo.require("dijit.layout.ContentPane");
    dojo.require("dijit.layout.BorderContainer");
    var map;
    function Init() {

        esri.config.defaults.io.proxyUrl = "../proxy.ashx"; 
        dojo.style(dojo.byId("map"), { width: dojo.contentBox("map").w + "px", height: (esri.documentBox.h - dojo.contentBox("navTable").h - 40) + "px" });
        map = new esri.Map("map");
        
        var CountyDataLayer = new esri.layers.ArcGISDynamicMapServiceLayer("http://servername/ArcGIS/rest/services/Counties/MapServer");
        map.addLayer(CountyDataLayer);

// the following service requires a token:
        var RestrictedLayer = new esri.layers.ArcGISDynamicMapServiceLayer("http://servername/ArcGIS/rest/services/Cadaster/CadasterLayerHalfTransparent/MapServer");

        map.addLayer(RestrictedLayer);
        
        
        var resizeTimer;
        dojo.connect(map, 'onLoad', function(theMap) {
            dojo.connect(dijit.byId('map'), 'resize', function() {
                clearTimeout(resizeTimer);
                resizeTimer = setTimeout(function() {
                    map.resize();
                    map.reposition();
                }, 500);
            });
        });
    }
    dojo.addOnLoad(Init);
</script>


and here's the code from the proxy.ashx page:

<?xml version="1.0" encoding="utf-8" ?>
<!-- Proxy config is used to set the ArcGIS Server services that the proxy will forward to.
        
        mustMatch: true to only proxy to sites listed, false to proxy to any site -->
<ProxyConfig mustMatch="true">
  <serverUrls>
    <!-- serverUrl options:
            url = location of the ArcGIS Server, either specific URL or stem
            matchAll = true to forward any request beginning with the url
            token = (optional) token to include for secured service
            dynamicToken = if true, gets token dynamically with username and
              password stored in this file.
            userName = the secured user's username
            password = the password for the username
            host = the machine name that is hosting the map service
    -->
    <serverUrl url="http://servername/ArcGIS/rest/services/Cadaster/CadasterLayerHalfTransparent/MapServer"
               matchAll="true"
               dynamicToken="true"
               host="servername"
               userName="UserName"
               password="ValidPassword"></serverUrl>
    <serverUrl url="http://www.google.com"
               matchAll="true"></serverUrl>
  </serverUrls>
  
</ProxyConfig>


I get no errors now.  I'm still not getting the service that requires a token though....

Any ideas?
0 Kudos
MatthewGerbrandt
New Contributor II
Thanks again, Harold.  Both of the links you suggested give me a blank web page.  Going into �??View Source�?� gives the following code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content="text/html; charset=windows-1252" http-equiv=Content-Type></HEAD>
<BODY></BODY></HTML>


No errors �?? no content.
0 Kudos
MatthewGerbrandt
New Contributor II
I just tried those links using FireFox and got the following error on the Google site:

XML Parsing Error: no element found
Location: http://servername/proxy.ashx?http://google.com
Line Number 1, Column 1:


I'm also getting an error in my proxy.config file when it's opened in Visual Studio.  The error is:

The ProxyConfig element is not declared
0 Kudos
SaurabhGupta
New Contributor II
What is the version of VS and .Net framework you are working with ?
Where have you deployed your proxy files ? Do you have read/execute permissions to that folder ?
0 Kudos
HaroldBostic
Occasional Contributor II
What happens when you go to:

http://PathToYourProxy/prox.ashx?http://www.google.com

I forgot the www in my last reply
0 Kudos