How to create a security on one page and use that security on another

1966
0
07-28-2015 11:08 AM
DirkVandervoort
Occasional Contributor II

We we all know, if you have a web map app that consumes secure services, when you navigate to that page the IdentityManager will provide a login dijit which creates a security.

I have a web page named MyWebAppWithSecureServices.html that meets that criteria.

I want my users to log in from another html page (MyLoginPage.html) and use that security to do a window.location.replace("MyWebAppWithSecureServices.html");

I want to do this without having to do any magic with a token, like appending it to the URL of a service REST endpoint.

I know I need to use "esri/Credential", "esri/IdentityManager", "esri/ServerInfo" to accomplish my goal. I'm having no success. Can you help me wrap my head around this please?

TIA

In the code below esriId is an instance of "esri/IdentityManager"

          function tryCreateCredentialsAndRedirectToAppWithSecureServices() {
            var userId = dojo.byId("username").value;
            var password = dojo.byId("password").value;

            var serverInfo = new ServerInfo();
            serverInfo.server = "http://www.MyDomain.com";
            serverInfo.tokenServiceUrl = "http://www.MyDomain.com/ArcGIS/tokens/";
            esriId.registerServers([serverInfo]);

            esriId.generateToken(serverInfo, {
                username: userId,
                password: password
            }).then(function(response) {
              // got our response from the token dispenser, now register it with the identify manager
              var tokenRegistrationObject = {
                expires: response.expires,
                server: serverInfo.server,
                ssl: false,
                token: response.token,
                userId: userId
              }
              esriId.registerToken(tokenRegistrationObject);

              var myCred = esriId.findCredential( "http://www.MyDomain.com", userId);

              var idObject = {};  
              idObject.serverInfos = serverInfo;  
              var credentials = {};  
              credentials.userId = userId;  
              credentials.server = "http://www.MyDomain.com/arcgis";
              credentials.token = response.token;  
              credentials.expires = response.expires;  
              credentials.ssl = false;  
              credentials.scope = "server";  
              credentials.validity = response.validity;  
              credentials.creationTime = myCred.creationTime;  
                
              idObject.credentials = [credentials];  
              esri.id.tokenValidity = response.validity;;  
              esri.id.initialize(idObject);  

              window.location.replace("MyWebAppWithSecureServices.html");


            });
          }    
0 Kudos
0 Replies