In this article, We learn how to create get access token for azure management API. I will show how to do it, and just follow the steps clearly.

For more updates please do Subscribe via Email:

Azure?

Azure is a cloud computing platform and an online portal that allows you to access and manage cloud services and resources provided by Microsoft. These services and resources include storing your data and transforming it, depending on your requirements

Access Token

In computer systems, an access token contains the security credentials for a login session and identifies the user, the user’s groups, the user’s privileges, and, in some cases, a particular application. In some instances, one may be asked to enter an access token rather than the usual password.

or

Access tokens are used in token-based authentication to allow an application to access an API. The application receives an access token after a user successfully authenticates and authorizes access, then passes the access token as a credential when it calls the target API.

Implementation

 
        public async Task<string> GetAccessTokenForAzureManagementAPI()
        {
            // Get configurations
            var tenantId = "Azure Active Directory Tenant ID";
            var azureManagementScope = "Azure Management API Endpoint Scope";
            string activeDirectoryClientId = "Active Directory Client Id";
            string activeDirectoryClientSecret = "Active Directory Client Secret";

            string accessToken = string.Empty;
            string accessTokenURL = $"https://login.microsoftonline.com/{tenantId}/oauth2/v2.0/token";

            // Build request body
            var requestBody = new Dictionary<string, string>();
            requestBody.Add("grant_type", "client_credentials");
            requestBody.Add("client_id", activeDirectoryClientId);
            requestBody.Add("client_secret", activeDirectoryClientSecret);
            requestBody.Add("scope", azureManagementScope);

            // Requesting a bearer token
            var httpContent = new FormUrlEncodedContent(requestBody);
            using (HttpClient httpClient = new HttpClient())
            {
                var response = await httpClient.PostAsync(new Uri(accessTokenURL), httpContent);
                var content = response?.Content?.ReadAsStringAsync().Result;

                if (response?.StatusCode == HttpStatusCode.OK || response?.StatusCode == HttpStatusCode.Created)
                {
                    JObject joResponse = JObject.Parse(content.ToString());
                    JValue ojObject = (JValue)joResponse["access_token"];

                    accessToken = ojObject.Value.ToString();
                }
            }

            return accessToken;
        }


        

Happy Learning..

Thank you for visiting my blog site. Hoping you learn more here. please feel free to comment and suggest if there is need to enhance and update. thank you.

Related Topics

Leave a Reply

Your email address will not be published. Required fields are marked *