Mason Wheeler
Mason Wheeler

Reputation: 84650

How to get an Azure access token (for an application, not a user) without involving user authentication?

Here's the scenario:

I've spent an hour now, searching all throughout the rabbit hole that is Microsoft's documentation on the subject, and I can't for the life of me figure out how to get an access token without the process for authenticating the user forcibly poking its nose into the workflow.

Does anyone know what the proper workflow is for the server app to get a token on behalf of itself using its own client ID/client secret, without the identities of any users being involved anywhere in the process? I don't find it believable that such an important workflow wouldn't exist; I just can't figure out what it is or how to do it. Any help would be appreciated.

Upvotes: 1

Views: 715

Answers (1)

Hury Shen
Hury Shen

Reputation: 15754

According to my understanding, you want to download things form Azure blob storage with Azure AD auth and you do not want to process the Azure AD auth with users, If so, I suggest you use service principal to process Azure AD auth then download files from Azure blob with the token.

For example

  1. create a service principal and assign Storage Blob Data Contributor for the sp.
az login
az account set --subscription "<your subscription id>"
# it will assign Storage Blob Data Contributor to the sp at subscription level
az ad sp create-for-rbac -n "mysample" --role Storage Blob Data Contributor

enter image description here

  1. Get access token
POST https://login.microsoftonline.com/<your tannat id>/oauth2/token 



Content-Type: application/x-www-form-urlencoded



grant_type=client_credentials
&client_id=<your sp appId>
&client_secret=<you sp password>
&resource=https://storage.azure.com/
  1. Download file with the token
Get <you blob url>



x-ms-version: 2017-11-09
Authorization: Bearer <access_token>

enter image description here

Besides, as @Gaurav said, if you deploy your project on Azure VM, you can enable Managed Identity for Vm then use the identity to access Azure storage. For more details, please refer to the document

Upvotes: 2

Related Questions