In the Content section of ArcGIS Portal, Add a new Application:
Enter the URL for the application (it must be HTTPS) and add a title and tags:
Go to the Settings page for the new application, and scroll down to the App Registration section. Use the Register button:
Enter the URL(s) for any pages/sites which will be using this application, then hit Add to add to the list. Hit Register when finished:
This will generate a unique AppID for this application:
Use that AppID in the demo at https://jsfiddle.net/slead/wb8e3mh9/, which uses the ArcGIS Portal to authenticate the user, before attempting to query a secure layer. This query step is a quick way to force the login prompt to appear.
Update the placeholder variables with the relevant values:
var appId = 'enter-the-app-id'
var portalUrl = 'enter-the-portal-url';
var testUrl = 'enter-the-full-path-to-the-secure-layer'