2017-02-21 2 views
0

Ich versuche eine stille Authentifizierung für Sharepoint Online in meine App zu implementieren. Ich habe den Office365-Dienst hinzugefügt. Ich benutze eine Testseite, die einfach 2 Textfelder mit dem Token und dem Ablaufdatum füllt. Ich habe den folgenden Code (nicht stille Auth), die ganz gut funktioniert:Authentifizierungskontext-Token-Cache ist nicht definiert

document.addEventListener('deviceready', function() { 
$(document).ready(function() { 
    var authContext = Microsoft.ADAL.AuthenticationContext; 
    authContext.createAsync("https://login.microsoftonline.com/common/") 
     .then(function (authContext) { 
      authContext.acquireTokenAsync(
       "https://my.sharepoint.com",  // Resource URI 
       "4be098f8-2184-4831-9ef7-3d17dbbef6a0",  // Client ID 
       "http://localhost:4400/services/office365/redirectTarget.html" // Redirect URI 
      ).then(function (authResult) { 
       $('#token').value = authResult.accessToken; 
       $('#expire').value = authResult.expiresOn; 
      }, function (err) { 
       console.log(err); 
      }); 
     }, function (err) { 
      console.log(err); 
     }); 
}); 
}); 

ich dann versuche, die stille Auth mit dem Code unten zu implementieren:

document.addEventListener('deviceready', function() { 
$(document).ready(function() { 
    var authContext = Microsoft.ADAL.AuthenticationContext; 
    authContext.tokenCache.readItems().then(function (items) { 
     if (items.length > 0) { 
      authority = items[0].authority; 
      authContext = new Microsoft.ADAL.AuthenticationContext(authority); 
     } 
     authContext.acquireTokenSilentAsync("https://my.sharepoint.com", "4be098f8-2184-4831-9ef7-3d17dbbef6a0").then 
     (function (authResult) { 
      $('#token').value = authResult.accessToken; 
      $('#expire').value = authResult.expiresOn; 
     }, 
     function (authContext) { 
      authContext.acquireTokenAsync(
       "https://my.sharepoint.com",  // Resource URI 
       "4be098f8-2184-4831-9ef7-3d17dbbef6a0",  // Client ID 
       "http://localhost:4400/services/office365/redirectTarget.html" // Redirect URI 
      ).then(function (authResult) { 
       $('#token').value = authResult.accessToken; 
       $('#expire').value = authResult.expiresOn; 
      }, function (err) { 
       console.log(err); 
      }); 
     }, function (err) { 
      console.log(err); 
     } 
     ) 
    }); 
}); 
}); 

ich Fehler bin immer bei dem Versuch, Um den Token-Cache zu lesen, erhalte ich den Token-Cache als undefiniert. Jedes Sample, das ich gesehen habe, bezieht sich auf den Token-Cache, also wundere mich, warum es undefiniert wäre?

Antwort

0

Ich musste async mein authContext-Objekt erstellen, bevor ich auf den tokenCache zugreifen konnte ... whoops