简体   繁体   中英

Generates the token but does not execute the azure url

I need to get this service from c # https://docs.microsoft.com/en-us/rest/api/power-bi-embedded/capacities/resume .

For this I use the GetAccessToken () function to obtain the authentication token.

public string GetAccessToken()
    {
        try
        {
            string tenantId = "ae4e3a81-xxxxxxxxxxxxxxxxxx";
            string clientId = "ca8393b2-xxxxxxxxxxxxxxxxxxxxxxxxx";
            string clientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
            string authContextURL = "https://login.windows.net/" + tenantId;
            var authenticationContext = new AuthenticationContext(authContextURL);
            var credential = new ClientCredential(clientId: clientId, clientSecret: clientSecret);
            var result = authenticationContext.AcquireTokenAsync(resource: "https://management.azure.com/", clientCredential: credential).Result;

            if (result == null)
            {
                throw new InvalidOperationException("Failed to obtain the JWT token");
            }

            string token = result.AccessToken.ToString();
            return token;

        }
        catch (Exception ex)
        {
            return ex.ToString();
        }
    }

Then I use the DoWork () method to use the token and be able to consume the azure ulr.

public void DoWork()
    {

        string Subscription = "afdbf38c-c33c-45ea-8e9b-===========";
        string token = GetAccessToken();
        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
            client.BaseAddress = new Uri("https://management.azure.com/");
            // Now you can party with your HttpClient!
            using (var response = client.PostAsync($"/subscriptions/{Subscription}/resourceGroups/Reportes/providers/Microsoft.PowerBIDedicated/capacities/powerbiembeddedjre1/resume?api-version=2017-10-01", null).Result)
            {
                response.EnsureSuccessStatusCode();
            }
        }
    }

But when I go to check the response it generates an error message:

{StatusCode: 403, ReasonPhrase: 'Forbidden', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: {Pragma: no-cache x-ms-failure-cause: gateway x-ms-request-id: 02d9df62-0529-4a7f-b492-36ce8584aad6 x-ms-correlation-request-id: 02d9df62-0529-4a7f-b492-36ce8584aad6 x-ms-routing-request-id: CANADACENTRAL: 20191114T163756Z: 02d9df62-052949-7 36ce8584aad6 Strict-Transport-Security: max-age = 31536000; includeSubDomains X-Content-Type-Options: nosniff Connection: close Cache-Control: no-cache Date: Thu, 14 Nov 2019 16:37:55 GMT Content-Length: 503 Content-Type: application / json; charset = utf-8 Expires: -1}}

What do I need to solve this problem?

looks like an issue with the bearer token, try client.SetBearerToken(token);

alternatively it's correctly sending back 403 because you do not meet the requirements for the claims (tenet/client issue).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM