0
votes

Recently, I face a Azure authenticate question, the error message as follows: Additional information: ForbiddenError: The server failed to authenticate the request. Verify that the certificate is valid and is associated with this subscription.

Here is code:

       private TokenCloudCredentials credentials;

        public ManagerClient(TokenCloudCredentials credentials)
        {
            this.credentials = credentials;
        }


        public void GetHostedServices()
        {
            var computeClient = new Microsoft.WindowsAzure.Management.Compute.ComputeManagementClient(credentials);
            var services = computeClient.HostedServices.List().HostedServices;

            foreach (var service in services)
            {
                Console.WriteLine("ServiceName: " + service.ServiceName);
                Console.WriteLine("Uri: " + service.Uri);
                Console.WriteLine();
            }
        }
        public static TokenCloudCredentials GetCredentials(string subscriptionId = "")
        {
            var token = GetAccessToken();
            if(string.IsNullOrEmpty(subscriptionId))
                subscriptionId = ConfigurationManager.AppSettings["subscriptionId"];
            var credential = new TokenCloudCredentials(subscriptionId, token);

            return credential;
        }
        private static string GetAccessToken()
        {
            AuthenticationResult result = null;

            var context = new AuthenticationContext(string.Format(
              ConfigurationManager.AppSettings["login"],
              ConfigurationManager.AppSettings["tenantId"]));

            result = context.AcquireToken(
              ConfigurationManager.AppSettings["apiEndpoint"],
              ConfigurationManager.AppSettings["clientId"],
              new Uri(ConfigurationManager.AppSettings["redirectUri"]));

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

            return result.AccessToken;
        }
              var credentials = Authorizator.GetCredentials("37a80965-5107-4f9b-91c6-a1198ee40226");
              new ManagerClient(credentials).GetHostedServices();
1

1 Answers

0
votes

var computeClient = new Microsoft.WindowsAzure.Management.Compute.ComputeManagementClient(credentials, new Uri("https://management.core.chinacloudapi.cn/"));

Default Uri is: https://management.core.windows.net so China Azure need to be changed to:***/management.core.chinacloudapi.cn/