[英]Azure API The server failed to authenticate the request
我有一個任務(我嘗試使用worker角色並上傳控制台應用程序並運行.exe),該任務應該每天運行一次並收集我的一些虛擬機的Azure Metrics。 這在本地完美無缺,但在雲服務上我得到此錯誤:
未處理的異常:Microsoft.WindowsAzure.CloudException:ForbiddenError:服務器無法驗證請求。 驗證證書是否有效並與此訂閱相關聯。 在Microsoft.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSucces ...等
發生這種情況的路線是:
MetricDefinitionListResponse metricListResponse = metricsClient.MetricDefinitions.List(resourceId, null,
nspace);
這是我的代碼的一部分:
string subscriptionId = "fc4xxxx5-835c-xxxx-xxx-xxxxxxx";
// The thumbprint of the certificate.
string thumbprint = "f5 b4 xxxxxxxx f7 c2";
// Get the certificate from the local store.
//X509Certificate2 cert = GetCertificate(StoreName.My, StoreLocation.LocalMachine, thumbprint);
//cert = GetCertificate(StoreName.My, StoreLocation.CurrentUser, thumbprint) ?? new X509Certificate2(("manageAzure.cer"));
var cert = new X509Certificate2(("manageAzure.cer"));
Console.WriteLine("Certificate is : " + cert);
// Create the metrics client.
var metricsClient = new MetricsClient(new CertificateCloudCredentials(subscriptionId, cert));
Console.WriteLine("metricsClient is : " + metricsClient);
// The cloud service name and deployment name, found in the dashboard of the management portal.
string cloudServiceName = "abms2-carlsberg";
string deploymentName = "abms2-carlsberg";
// Build the resource ID string.
string resourceId = ResourceIdBuilder.BuildVirtualMachineResourceId(cloudServiceName, deploymentName);
string nspace = "WindowsAzure.Availability";
// Get the metric definitions.
MetricDefinitionListResponse metricListResponse = metricsClient.MetricDefinitions.List(resourceId, null,
nspace);
我已將管理證書放在我的解決方案中,然后從那里加載它(它設置為始終復制)並且在本地運行時使用的是相同的(和相同的方式)。
那么什么“證書”抱怨“認證”? 我似乎無法看到問題所在。 任何幫助將非常感謝,因為我已經使用了整個下午!
PS:我已經在高架模式下運行了!
對於可能有這個問題的其他人,我已經解決了它,如下面的解釋:( http://www.dinohy.com/post/2013/11/12/403-Forbidden-when-Use-Azure-Management -REST-API-on-Role-instance.aspx )
從以下網址下載publishsettings文件: https ://manage.windowsazure.com/publishsettings/index?client = vs & schemaversion = 2.0(這是一個XML文件,你可以用記事本打開它)
找到ManagementCertificate屬性,將值復制到字符串。 那個Base64編碼的字符串,你可以使用這個字符串創建一個證書:string base64Cer =“ManagementCertificate的值”
使用此字符串創建證書。 var certificate = new X509Certificate2(base64Cer);
雖然最后一步並不完全像直接傳遞字符串(因為字符串太長而導致異常),而是如下所示:var cert = new X509Certificate2(Convert.FromBase64String(base64cer));
希望這也能幫助我的位置上的其他人。
猜測......您正在加載用於從.cer文件驗證自己的證書。 它沒有私鑰,因此無法用於驗證您。 我懷疑在本地你可能有私鑰存儲在你的私人證書存儲中,假設你在你的機器上生成了證書,這可能使它在本地工作。
簡而言之,嘗試使用pfx文件而不是cer。 pfx for中包含私鑰。 如果您使用makecert在計算機上生成證書,則最初只有.cer文件。 使用本地證書管理器在個人存儲中查找證書,然后將其導出到pfx文件並包含私鑰。
這種方法對我有幫助......
public static X509Certificate2 GetCertificate(string certificateString)
{
if (string.IsNullOrEmpty(certificateString))
return null;
var certificateAsBytes = Convert.FromBase64String(certificateString);
var certificate = new X509Certificate2(certificateAsBytes);
return certificate;
}
我想出了另一個場景。 訂閱ID不匹配時發生錯誤。
證書:
<Subscription
ServiceManagementUrl="https://management.core.windows.net"
Id="00000000-0000-0000-0000-000000000000" /* -- Subscription Id -- */
Name="Visual Studio Premium with MSDN"
ManagementCertificate="" />
我正在嘗試獲得這樣的憑據
碼:
string subscriptionId = "11111111-1111-1111-1111-111111111111"; // Subscription Id...
var credentials = new CertificateCloudCredentials(subscriptionId, x509Certificate2); // Will be varied here...
我的訂閱ID不匹配的位置。 因此,當我嘗試使用“證書”驗證我的請求時收到此異常。
希望這可以幫助...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.