Hello, Warren.
We have a customized web appbuilder application that has protected widgets and protected maps.That function helpped me to login on our portal for arcgis without the need of the default popup of authentication.
If the default popup works for you, you could just use something like this snippet:
To test either this snippet or the previous function you could access your web appbuilder app and put them on the browser debugger.In both casses, after the authentication completes, the global object esri.id.credentials[0].token will have the token to use for subsequent requests to protected resources.
Hello Carlos
Can you provide some further details on where and how this was implemented?
Thanks so much,
Warren
here is the code we use to do a hardcoded login
function initPredefinedSecurity() { //alert('in initPredefinedSecurity'); serverInfo = new ServerInfo(); serverInfo.server = 'https://'+window.location.hostname+'/arcgis03'; serverInfo.tokenServiceUrl = 'https://'+window.location.hostname+'/arcgis03/tokens/generateToken'; serverInfo.shortLivedTokenValidity = 720; esri.id.registerServers([serverInfo]); //add your own password var def = esri.id.generateToken(serverInfo, {"username": "util", "password": ""}); def.addCallback(lang.hitch(this, function (tokenInfo) { //get token creation time in epoch var creationTime = (new Date).getTime(); //calculate the token expiration based on short lived token validity var expirationTime = creationTime + (serverInfo.shortLivedTokenValidity * 60000); //create array of secured services var idObject ={}; idObject.serverInfos= [serverInfo]; var credentials={}; credentials.userId = "util"; credentials.server = "https://"+window.location.hostname+"/arcgis03"; credentials.token = tokenInfo.token; credentials.expires = expirationTime; credentials.ssl = false; credentials.scope = "server"; credentials.validity = 720; credentials.creationTime = creationTime; idObject.credentials = [credentials]; //credential object is correct esri.id.initialize(idObject); // now create map initMap(); })); };
var ex = require("express"); var app = ex(); var http = require("http"), qs = require("querystring"); var userToken, userTokenExpire; var getToken = function(userInfo, res){ var currentTime = new Date().getTime(); if (userToken && (userTokenExpire && userTokenExpire - 7200000 > currentTime) ) { //-7200000 (it's 2 hours) ensures the token is valid for at least next 2 hours. //any new request within the 2 hours will create a new token. res.send({token: userToken, expires: userTokenExpire}); } else { var options = { method: "POST", host: /*ip address here*/, path: "http://arcgisserverdomain/arcgis/admin/generateToken", headers: {"Connection": "keep-alive", "host": /*ip address*/, "Content-Type": "application/x-www-form-urlencoded"} }; var postData = qs.stringify({ username: userInfo.username, password: userInfo.password, client: "referer", referer: "http://yourwebsitedomain/", f: "json", expiration:1440 //it can be up to 24 hours, which is 24*60 minutes }); var request = http.request(options, function(response){ response.setEncoding('utf8'); response.on('data', function (chunk) { userToken = JSON.parse(chunk).token; console.log(userToken); userTokenExpire = JSON.parse(chunk).expires; console.log(userTokenExpire); res.send(chunk); }); }); request.write(postData); request.end(); } }; app.get("/gettoken", function(req, res){ var userInfo = {}; //userInfo.username = req.query.username; //userInfo.password = req.query.password; //if (!userInfo.username && !userInfo.password) { //since you don't need user to provide username and password, hard-code it. userInfo.password = username; userInfo.username = password; //} getToken(userInfo, res); }); app.listen(3000);
Membros conectados podem postar, seguir atualizações e mais. Novo aqui? Registre uma conta gratuita.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.