Azure API failed to authenticate the request

我只是一个虾纸丫 提交于 2020-01-16 01:05:20

问题


I am using the next code to get the token for Azure AD authentication

errorMessage = "";
        AuthenticationResult result = null;
        var context = new AuthenticationContext(String.Format(CultureInfo.InvariantCulture, ConfigurationManager.AppSettings["login"], ConfigurationManager.AppSettings["tenantId"]),false);
        ClientCredential clientCredential = new ClientCredential(ConfigurationManager.AppSettings["clientId"], ConfigurationManager.AppSettings["key"]);
        try
        {
            result = context.AcquireToken(ConfigurationManager.AppSettings["apiEndpoint"], clientCredential);
        }
        catch (AdalException ex)
        {
            if (ex.ErrorCode == "temporarily_unavailable")
            {
                errorMessage = "Temporarily Unavailable";
                return null;
            }
            else
            {
                errorMessage = "Unknown Error";
                return null;
            }
        }
        string token = result.AccessToken;
        var credential = new TokenCloudCredentials(ConfigurationManager.AppSettings["subscriptionId"],token);
        //string certificateString = ConfigurationManager.AppSettings["managementCertificate"];
        //var cert = new X509Certificate2(Convert.FromBase64String(base64cer));
        return credential;

After that I am doing the next to create a website in Azure

            using (var computeClient = new WebSiteManagementClient(credentials))
        {
            var result = computeClient.WebSites.IsHostnameAvailable(websiteName);
            if (result.IsAvailable)
            {
                await computeClient.WebSites.CreateAsync(WebSpaceNames.WestEuropeWebSpace, new WebSiteCreateParameters() {
                    Name= websiteName,
                    ServerFarm= ConfigurationManager.AppSettings["servicePlanName"]
                });
            }
            else
            {
                return ResultCodes.ObjectNameAlreadyUsed;
            }
        }

But every time I execute that I got the following error:

ForbiddenError: The server failed to authenticate the request. Verify that the certificate is valid and is associated with this subscription.

I tried to import the management certificate as they said here: https://www.simple-talk.com/cloud/security-and-compliance/windows-azure-management-certificates/

And also tried this one: http://technetlibrary.com/change-windows-azure-subscription-azure-powershell/198

For importing management certificate.
Also I gave the application permissions to access management API.


回答1:


Azure AD Authentication DOES NOT use the management certificate authentication.

There is a good documentation and code sample on MSDN on how to resolve your current issue. Authenticating Service Management Requests




回答2:


Looks like your application does not have permission to access the Azure API's. Please use this link to get permissions. After this please add permissions to access API in app permission or user permission.



来源:https://stackoverflow.com/questions/33450610/azure-api-failed-to-authenticate-the-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!