Why do i need to create a Multi-Tenant App?

2019-09-02 20:34发布

问题:

I have been doing some R&D on using the MicrosoftGraphAPI to fetch the skus subscribed by my organization.

I have created an app as described in the documentation. I did all the steps in the above link except 'Assign application to role'.

Using postman am able to get the oauth2 token by sending a post request using the link https://login.microsoftonline.com/<mytenantid>/oauth2/token with the client_id, client_secret, resource(https://graph.microsoft.com) and grant_type(client_credentials) parameters.

After this token is obtained I can fire a get request https://graph.microsoft.com/v1.0/subscribedSkus with the Authorization header set as Bearer {token} which will return the SKUs subscribed by my organization. So far so good. :-)

Now the requirement is I need to fetch the subscribed SKUs by one of the client (let's say having the azure ad tenant id 'ABCDEFG') of my organization. I can successfully do that by registering an app in the client's tenant 'ABCDEFG' with the same steps as above. This approach is fine if my organization has say 1 or 2 clients. However, if the client numbers are more than say 30 this approach of registering an application in each Azure AD instance is not feasible.

If the application that I registered in my organizations AAD was multi-tenant then how should it help me? What will be the steps needed to obtain the access token for each tenant? Can somebody assist with some detailed explanation?

回答1:

Since you need application-level access, you would assign one of the Application permissions listed in the documentation for getting SKUs: https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/subscribedsku_list.

Directory.Read.All, Directory.ReadWrite.All

In this case you should require the Read Directory Data (Directory.Read.All) application permission.

Then you mark your app as multi-tenanted.

Now then in order for another org to use your app, they will have to be on-boarded. You will need some kind of page where their administrator can click a button/link to start using your app. This should redirect the admin to:

https://login.microsoftonline.com/common/oauth2/authorize?client_id=your-client-id&prompt=admin_consent&response_type=code+id_token&redirect_uri=url-where-to-send-user-back

Once they sign in, they will be presented with a consent screen, where they can approve the permissions that your app requires. If and when they do that, they will be redirected back to your app (to the URL you specified) and you can use the Id token to know which Azure AD tenant registered.

During this process a service principal for your app is created in their tenant, and the required permission is granted to it. This means you can then get an access token for their tenant from: (using the same credentials)

https://login.microsoftonline.com/their-tenant-id/oauth2/token

Remember that access tokens are specific to an Azure AD tenant, so you will have to get an access token for each tenant.

One thing I would like to point out is that you should instead try to use delegated permissions if possible. The application permission given here gives quite large access to your app, and some admins might not use your service for that reason alone. Delegated permissions are more complex to handle, but allow your app to act on behalf of a user instead of purely as itself.