Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- OurNamespace = {
- serviceMainUrl: "https://localhost:44339",
- webApiAppIdUri: "WEB-API-URI",
- tenant: "TENANT",
- clientId: "NATIVE-APP-GUID", // Native APP
- adalEndPoints: {
- get: null
- },
- adal: null,
- authContext: null,
- dummyAuthPage: null,
- usePopup: true,
- init: function () {
- this.dummyAuthPage = "DummmyLogin.aspx";
- OurNamespace.adalEndPoints.get = OurNamespace.serviceMainUrl + "/api/values";
- OurNamespace.authContext = new AuthenticationContext({
- tenant: OurNamespace.tenant + '.onmicrosoft.com',
- clientId: OurNamespace.clientId,
- webApiAppIdUri: OurNamespace.webApiAppIdUri,
- endpoints: OurNamespace.adalEndPoints,
- popUp: false,
- postLogoutRedirectUri: window.location.origin,
- cacheLocation: "localStorage",
- displayCall: OurNamespace.displayCall,
- redirectUri: _spPageContextInfo.siteAbsoluteUrl + "/Pages/" + OurNamespace.dummyAuthPage
- });
- var user = OurNamespace.authContext.getCachedUser(); // OurNamespace.authContext.getCachedToken(OurNamespace.clientId)
- if (user) {
- OurNamespace.azureAdAcquireToken();
- } else {
- OurNamespace.initLogin();
- }
- },
- initLogin: function () {
- //OurNamespace.authContext.config.displayCall = null;
- //var isCallback = OurNamespace.authContext.isCallback(window.location.hash);
- //OurNamespace.authContext.handleWindowCallback();
- //if (isCallback && !OurNamespace.authContext.getLoginError()) {
- // window.location.href = OurNamespace.authContext._getItem(OurNamespace.authContext.CONSTANTS.STORAGE.LOGIN_REQUEST);
- //}
- OurNamespace.authContext.login();
- },
- displayCall: function (url) {
- var iframeId = "azure-ad-tenant-login",
- popup = null;
- if (OurNamespace.usePopup) {
- popup = window.open(url, 'auth-popup', 'width=800,height=500');
- } else {
- var iframe = document.getElementById(iframeId);
- if (!iframe) {
- iframe = document.createElement("iframe");
- iframe.setAttribute('id', iframeId);
- document.body.appendChild(iframe);
- }
- iframe.style.visibility = 'hidden';
- iframe.style.position = 'absolute';
- iframe.src = url;
- popup = iframe.contentDocument;
- }
- var intervalId = window.setInterval(function () {
- try {
- // refresh the contnetDocument for iframe
- if (!OurNamespace.usePopup)
- popup = iframe.contentDocument;
- var isCallback = OurNamespace.authContext.isCallback(popup.location.hash);
- OurNamespace.authContext.handleWindowCallback(popup.location.hash);
- if (isCallback && !OurNamespace.authContext.getLoginError()) {
- popup.location.href = OurNamespace.authContext._getItem(OurNamespace.authContext.CONSTANTS.STORAGE.LOGIN_REQUEST);
- window.clearInterval(intervalId);
- if (OurNamespace.usePopup) {
- popup.close();
- }
- var user = OurNamespace.authContext.getCachedUser();
- if (user) {
- console.log(user);
- } else {
- console.error(OurNamespace.authContext.getLoginError());
- }
- }
- } catch (e) { }
- }, 400);
- },
- azureAdAcquireToken: function () {
- OurNamespace.authContext.acquireToken(OurNamespace.adalEndPoints.get, function (error, token) {
- if (error || !token) {
- SP.UI.Status.addStatus("ERROR", ('acquireToken error occured: ' + error), true);
- return;
- } else {
- OurNamespace.processWebApiRequest(token);
- }
- });
- },
- processWebApiRequest: function (token) {
- // Alternatively, in MVC you can retrieve the logged in user in the web api with HttpContext.Current.User.Identity.Name
- $.ajax({
- type: "GET",
- url: OurNamespace.adalEndPoints.get,
- contentType: "application/json; charset=utf-8",
- dataType: "json",
- data: {},
- headers: {
- 'Authorization': 'Bearer ' + token
- },
- success: function (results) {
- var dataObject = JSON.parse(results);
- SP.UI.Status.addStatus("Info", "ADAL GET data success: " + dataObject.webApiUser);
- $(".grid-info").html("<h1 class="h1">Current Web API authenticated user: " + dataObject.webApiUser + "</h1>");
- },
- error: function (xhr, errorThrown, textStatus) {
- console.error(xhr);
- SP.UI.Status.addStatus("ERROR", ('Service error occured: ' + textStatus), true);
- }
- });
- }
- }
Add Comment
Please, Sign In to add comment