         XML.ignoreWhitespace = true;
         XML.prettyIndent = 4;
 
-        IdentityManager.instance.enabled = true;
-
-        ToolTipManager.toolTipClass = com.esri.builder.components.ToolTip;
+        var portalURL:String = PortalModel.getInstance().portalURL;
+        var idManager:IdentityManager = IdentityManager.instance;
+
+        idManager.enabled = true;
+
+        if (PortalModel.getInstance().isAGO(portalURL))
+        {
+            idManager.addEventListener(IdentityManagerEvent.SHOW_OAUTH_WEB_VIEW, showOAuthWebViewHandler);
+            PortalModel.getInstance().registerOAuthPortal(portalURL);
+            idManager.getCredential(portalURL, false, new Responder(getCredentialOutcomeHandler,
+                                                                    getCredentialOutcomeHandler));
+
+            function getCredentialOutcomeHandler(outcome:Object):void
+            {
+                idManager.removeEventListener(IdentityManagerEvent.SHOW_OAUTH_WEB_VIEW, showOAuthWebViewHandler);
+            }
+
+            function showOAuthWebViewHandler(event:IdentityManagerEvent):void
+            {
+                idManager.removeEventListener(IdentityManagerEvent.SHOW_OAUTH_WEB_VIEW, showOAuthWebViewHandler);
+                event.preventDefault();
+                idManager.setCredentialForCurrentSignIn(null);
+            }
+        }
+
+        ToolTipManager.toolTipClass = ToolTip;
 
         // Can only have access to 'loaderInfo' when the app is complete.
         app.loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, uncaughtErrorHandler);

import mx.managers.ToolTipManager;
import mx.resources.ResourceManager;
import mx.rpc.AsyncResponder;

import spark.components.WindowedApplication;


{

import com.esri.ags.components.IdentityManager;
import com.esri.ags.events.IdentityManagerEvent;
import com.esri.builder.components.LogFileTarget;
import com.esri.builder.components.ToolTip;
import com.esri.builder.controllers.supportClasses.MachineDisplayName;
