AnsweredAssumed Answered

Bypass user login dialog and automatically signing a user account

Question asked by scottgrace on Jun 28, 2013
Latest reply on Dec 26, 2013 by rmai
I need to automatically sign in a user without requiring them to enter the username and password for the ArcGIS online account. I have a user name and password that i want to be able to use everytime a user tries to view a web app that is only viewable to an organization. When it is only visible to the organization you are required to sign in, i want to bypass the process and automatically login a user. I know that i need to use the token feature but im unable to make that work... here is what i have:

<script type="text/javascript">
      dojo.require("utilities.App");
      dojo.require("templateConfig.commonConfig");
      
      
        dojo.require("dojo.parser");
        dojo.require("esri.IdentityManager");
        var cred = "esri_jsapi_id_manager_data";
        var shortLivedTokenValidity=60;
        function init() {

            var idBase = new esri.IdentityManagerBase();
            //esri.config.defaults.io.proxyUrl = "proxy.ashx";
            var serverInfo = {
                server: "http://myserver:port",
                tokenServiceUrl: "http://myserver/arcgis/tokens",
                currentVersion: 10.1
            };
            var def = idBase.generateToken(serverInfo, { username: "user1", password: "user1" });
            def.addCallback(function (tokenInfo) {
        var idBase = new esri.IdentityManagerBase();
        //Short lived token is valid for 60 mins by defult 
                idBase.tokenValidity =shortLivedTokenValidity=60;
                var serverInfo = {
                    server: "http://myserver:port",
                    tokenServiceUrl: "http://myserver/arcgis/tokens/",
                    currentVersion: 10.1
                };
                
                esri.id.registerServers([serverInfo]);
               //get token creation time in epoch
                var creationTime = (new Date).getTime();
        //calculate the token expiration based on short lived token validity
                var expirationTime = creationTime + (shortLivedTokenValidity * 60000);
        //create array of secured services 
                var securedServices = [];
                for (var services in this.configData.mapService) {
                    securedServices.push(this.configData.mapService[services]);
                }
                var idString = dojo.toJson({ serverInfos: [serverInfo],
                    credentials: [{
                        userId: user1,
                        server: "http://myserver:port",
                        token: tokenInfo.token,
                        expires: expirationTime,
                        ssl: false,
                        creationTime: creationTime,
                        resources: securedServices
                    }]
                });
                // store it client side
                if (_supports_local_storage()) {
                    // use local storage

                    window.localStorage.setItem(this._jsAPIIDManagerData, idString);
                } else {
                    // use a cookie
                    dojo.cookie(this._jsAPIIDManagerData, idString, { expires: 1 });
                }
                this._loadCredentials();
        
      });
      
               
        }
    
     function _supports_local_storage() {
            try {
                return "localStorage" in window && window["localStorage"] !== null;
            } catch (e) {
                return false;
            }
        }

        function _loadCredentials() {

            var idJson, idObject;
            if (this._supports_local_storage()) {
                // read from local storage
                idJson = window.localStorage.getItem(this._jsAPIIDManagerData);
            } else {
                // read from a cookie
                idJson = dojo.cookie(this._jsAPIIDManagerData);
            }
            if (idJson && idJson != "null" && idJson.length > 4) {
                idObject = dojo.fromJson(idJson);
                esri.id.initialize(idObject);
            }
        }
        
    
    dojo.addOnLoad(init);


      var i18n;
      dojo.ready(function(){

      
        i18n = dojo.i18n.getLocalization("esriTemplate","template");
        var  defaults = {
        //The ID for the map from ArcGIS.com
        webmap: "mywebmapid",
        //Modify this to point to your sharing service URL if you are using the portal
        sharingurl: null,//for example: "http://www.arcgis.com",
        //The id for the web mapping application item that contains configuration info - in most
        ////When editing you need to specify a proxyurl (see below) if the service is on a different domain
        //Specify a proxy url if you will be editing, using the elevation profile or have secure services or web maps that are not shared with everyone.
        proxyurl: "",
        //cases this will be null.
        appid: "",
        //set to true to display the title
        displaytitle: true,
        //Enter a title, if no title is specified, the webmap's title is used.
        title: "",
        //Enter a description for the application. This description will appear in the left pane
        //if no description is entered the webmap description will be used.
        description: "",
        //specify an owner for the app - used by the print option. The default value will be the web map's owner
        owner: '',
        //Specify a color theme for the app. Valid options are gray,blue,purple,green and orange
        theme: 'gray',
        //Optional tools - set to false to hide the tool
        //set to false to hide the zoom slider on the map
        displayslider: true,
        displaymeasure: true,
        displaybasemaps: true,
        displayoverviewmap: true,
        displayeditor: true,
        displaylegend: true,
        displaysearch: true,
        displaylayerlist: true,
        displaybookmarks: true,
        displaydetails: true,
        displaytimeslider: true,
        displayprint: true,
        
        displayelevation: false,
        //This option is used when the elevation chart is displayed to control what is displayed when users mouse over or touch the chart. When true, elevation gain/loss will be shown from the first location to the location under the cursor/finger.
        showelevationdifference: false,
        displayscalebar: true,
        displayshare: true,
        //Set to true to display the left panel on startup. The left panel can contain the legend, details and editor. Set to true to
        //hide left panel on initial startup. 2
        leftPanelVisibility: true,
        //If the webmap uses Bing Maps data, you will need to provide your Bing Maps Key
        bingmapskey: commonConfig.bingMapsKey,
        //specify a group in ArcGIS.com that contains the basemaps to display in the basemap gallery
        //example: title:'ArcGIS Online Basemaps' , owner:esri
        basemapgroup: {
          title: null,
          owner: null
        },
        //Enter the URL's to the geometry service, print task and geocode service. 
        helperServices: commonConfig.helperServices,
        //Set the label in the nls file for your browsers language
        printlayouts: [{
          layout: 'Letter ANSI A Landscape',
          label: i18n.tools.print.layouts.label1,
          format: 'PDF'
        }, {
          layout: 'Letter ANSI A Portrait',
          label: i18n.tools.print.layouts.label2,
          format: 'PDF'
        }, {
          layout: 'Letter ANSI A Landscape',
          label: i18n.tools.print.layouts.label3,
          format: 'PNG32'
        }, {
          layout: 'Letter ANSI A Portrait',
          label: i18n.tools.print.layouts.label4,
          format: 'PNG32'
        }],
        printlayout: false,
        printformat: "PNG32",
        //Specify the geocoder options. By default uses the geocoder widget with the default locators. If you specify a url value then that locator will be used.
        placefinder: {
          "url": "",
          "countryCode":"",
          "currentExtent":false,
          "placeholder": "",
          "singlelinefieldname":""
        },
        
        link1: {
          url: '',
          text: ''
        },
        link2: {
          url: '',
          text: ''
        },
        //specify the width of the panel that holds the editor, legend, details
        leftpanewidth: 228,
        //Restrict the map's extent to the initial extent of the web map. When true users
        //will not be able to pan/zoom outside the initial extent.
        constrainmapextent: false,
        //Provide an image and url for a logo that will be displayed as a clickable image
        //in the lower right corner of the map. If nothing is specified then the esri logo will appear.
        customlogo: {
          image: '',
          link: ''
        },
        //embed = true means the margins will be collapsed to just include the map no title or links
        embed: false
      };
        
        var app = new utilities.App(defaults);
        app.init().then(function(options){
            initMap(options);
        });

      });

    </script>







The could for the token generation was originally posted here:
http://forums.arcgis.com/threads/73298-Possible-to-hard-code-credentials?highlight=token

and the rest of the code you see is from the basic viewer

Outcomes