Authenticating using OfficeDev/office-js-helpers rather than adal

心已入冬 提交于 2019-12-10 17:55:01

问题


I'm working on an Office Add-in that currently uses adal to obtain an auth token.

As I want to use the Fabric front end I am changing it to React and I notice that the officer-js-helpers have implemented authenticators that seem to do the same job as the adal library. Am I correct in this assumption? If so, how do I duplicate this adal config using the office-js-helpers authentication functions:

var adalConfig = {
    instance: 'https://login.microsoftonline.com/',
    tenant: 'myprivatesite.onmicrosoft.com',
    clientId: 'xxx-xxx-xxx-xxx-xxx',
    endpoints: {
      'https://my.private.url.endpoint/path': 'https://myprivatesite.onmicrosoft.com/path.to.something',
    }

And this token request:

var authContext = new AuthenticationContext(adalConfig);
 authContext.acquireToken('https://myprivatesite.onmicrosoft.com/path.to.something', function (error, token) {
        console.log(token)
      });

UPDATE: I have got the adal.js library working in my react app. I have used some of the code from the init function in the adalAuthenticationService angular provider to retrieve the authentication token.

So the question remains. Can I use the office-js-helpers to do the same thing?


回答1:


Adal.js cannot be used out of the box for web add-ins authentication because within the sandboxed iFrame context of a web add-ins you cannot navigate simply to the authentication login page hosted outside your domain.

Office-js-helpers uses the dialogAPI when available and a popup as a fallback solution when not available.

If I remember correctly Office-js-helpers targets only Azure AD v2.0 (which comes with a lot of nice new features comparing to Azure AD). I guess it is a good choice.

I created an Open source sample the documentation can be interesting to you. However, this is not exactly what you want it is based on an AuthorizationCode flow while you are looking for Implicit flow.




回答2:


OK It appears it is extremely easy. All that is required from the adal configuration is the client Id and the tenant.

if (OfficeHelpers.Authenticator.isAuthDialog()) {
  return;
}

var authenticator = new OfficeHelpers.Authenticator();

authenticator.endpoints.registerAzureADAuth('xxx-xxx-xxx-xxx-xxx', //clientId
'myprivatesite.onmicrosoft.com' // tenant
);

authenticator.authenticate(OfficeHelpers.DefaultEndpoints.AzureAD)
  .then(function (token) {
    console.log(token);
  .catch(function(error) {
    console.log(error);
  });


来源:https://stackoverflow.com/questions/40674084/authenticating-using-officedev-office-js-helpers-rather-than-adal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!