How to handle basic Web authentication with esri/request?

2512
8
10-27-2016 01:19 PM
BryanChastain
New Contributor III

I'm trying to call esri/request on a page that requires username/password, but when I do, I get the following error:

esri.config.defaults.io.proxyUrl is not set. If making a request to a CORS enabled server, please push the domain into esri.config.defaults.io.corsEnabledServers
Error: "Unable to load https://<myserver> status: 0"

But if I do add the domain into corsEnabledServers, I get this error:

XMLHttpRequest cannot load https://<myserver>. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. The response had HTTP status code 401.
Error: Unable to load https://<myserver>' status: 0

Any ideas?

0 Kudos
8 Replies
RobertScheitlin__GISP
MVP Emeritus

Bryan,

  Sure that is when you need to setup a proxy for your app:

Setting up a Proxy | Support Services Blog 

It will handle the authentication for you and all will be good to go.

0 Kudos
BryanChastain
New Contributor III

Even if it's not esri authentication? It's just simple web authentication, unrelated to esri's AGS/AGOL stuff. I've used the esri resource proxy before, but only on esri resources...

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Bryan,

   Oh, sorry I did not understand that the request was not going to ArcGIS Server. In that case you will need to get a proxy that pass basic authentication with your request or use

esri/request | API Reference | ArcGIS API for JavaScript 3.18 | setRequestPreCallback 

to add your request end point required info.

I looked for something to do this a while back and could not find anything.

0 Kudos
BryanChastain
New Contributor III

Not sure what I would do in the setRequestPreCallback though.

0 Kudos
DanMcCoy
Occasional Contributor III

There's a branch of ESRI's resource-proxy that supports HTTP Basic Authentication for non-ArcGIS Services:

GitHub - sverkerEsriSE/resource-proxy at add-support-for-basic-auth 

I was able to get the .NET version working to connect to Oracle Service Cloud's REST API.

resource-proxy/README.md at add-support-for-basic-auth · sverkerEsriSE/resource-proxy · GitHub 

0 Kudos
AkshayHarshe
Esri Contributor

Hey Bryan,

If you want to use setRequestPrecallback function: Please find following samples

developer-support/web-js/Using-setRequestPreCallback-function at gh-pages · Esri/developer-support ·... 

developer-support/web-js/setRequestPreCallback-Modify-Print-Lengend at gh-pages · Esri/developer-sup... 

This function will intercept any request going out from your application and you can dynamically modify this request to pass what you need to pass.

Thanks,

Akshay

Thanks,
Akshay Harshe
BryanChastain
New Contributor III

Thanks Akshay. However, I'm still not sure how I would set up the authentication at that point.

0 Kudos
AkshayHarshe
Esri Contributor

I am not sure if you have figured this out but you should be able to send username and password before the request is send to the server. But in my opinion that is not the best way to do since your credentials are exposed.

Thanks,
Akshay Harshe
0 Kudos