Azure API服务器无法validation请求

我有一个任务(我尝试使用worker角色并上传控制台应用程序并运行.exe),该任务应该每天运行一次,并收集我的一些虚拟机的Azure Metrics。 这在本地完美无缺,但在云服务上我得到此错误:

未处理的exception:Microsoft.WindowsAzure.CloudException:ForbiddenError:服务器无法validation请求。 validation证书是否有效并与此订阅相关联。 在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 )

  1. 从以下url下载publishsettings文件: https ://manage.windowsazure.com/publishsettings/index?client = vs & schemaversion = 2.0(这是一个XML文件,你可以用记事本打开它)

  2. 找到ManagementCertificate属性,将值复制到字符串。 那是一个Base64编码的字符串,你可以使用这个字符串创建一个证书:string base64Cer =“ManagementCertificate的值”

  3. 使用此字符串创建证书。 var certificate = new X509Certificate2(base64Cer);

虽然最后一步并不完全像直接传递字符串(因为字符串太长而导致exception),而是如下所示:var cert = new X509Certificate2(Convert.FromBase64String(base64cer));

希望这也能帮助我的位置上的其他人。

猜测……您正在加载用于从.cer文件validation自己的证书。 它没有私钥,因此不能用于validation您。 我怀疑在本地你可能有私钥存储在你的私人证书存储中,假设你在你的机器上生成了证书,这可能使它在本地工作。

简而言之,尝试使用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不匹配时发生错误。

证书:

  

我正试图获得这样的凭据

码:

 string subscriptionId = "11111111-1111-1111-1111-111111111111"; // Subscription Id... var credentials = new CertificateCloudCredentials(subscriptionId, x509Certificate2); // Will be varied here... 

我的订阅ID不匹配的位置。 因此,当我尝试使用“证书”validation我的请求时收到此exception。

希望这可以帮助…