Authenticate against an Azure Mobile Service App with ADAL.js acquired token

前端 未结 3 734
南方客
南方客 2021-01-12 23:30

I\'m trying to authenticate a HTML app against an Azure Mobile Service app.

The Setup

Both apps use AAD as authentication backend, so both apps have an app

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-12 23:35

    You can use the AzureMobileServices client script to do a login with an already obtained token:

    You need to include the follwing script: https://ajax.aspnetcdn.com/ajax/mobileservices/MobileServices.Web-1.2.8.min.js

    Then after you obtained the token with ADAL.JS you can use it to login and obtain a Mobile Service Authentication Token:

    var appUrl = 'https://foobar.azure-mobile.net'
      , appKey = 'zumo key' // found on the dashboard of the mobile service
      , client = new WindowsAzure.MobileServiceClient(appUrl, appKey);
    
    // ...
    var token = this.getAADToken(ZUMOAuthenticationProvider.Config().url);
    
    client
      .login('aad', { 'access_token': token })
      .then(function() {
        // client.currentUser.mobileServiceAuthenticationToken
      });
    

    This token then needs to be included in succeeding mobile service API requests:

    var config = {
      headers: {
        'X-ZUMO-AUTH': client.currentUser.mobileServiceAuthenticationToken
      }
    }
    
    $http
      .get(appUrl + '/some/path', config)
      .then(function (r) {
         console.log(r);
      });
    

提交回复
热议问题