Man*_*ini 7 c# certificate azure x509certificate azure-worker-roles
我有一个任务(我尝试使用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:我已经在高架模式下运行了!
Man*_*ini 10
对于可能有这个问题的其他人,我已经解决了它,如下面的解释:(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));
希望这也能帮助我的位置上的其他人.