获取Azure中托管服务中的实例列表

获取Azure中托管服务中的实例列表,azure,.net-core,azure-cloud-services,Azure,.net Core,Azure Cloud Services,我在Azure中有一个工作者角色,我想列出C#dotnet core中的所有实例。 有几个azure管理nuget包。我已经测试了其中一些,但没有一个给出预期的结果。 我还尝试了本地RESTAPI,但找不到可用的示例 有人可以发布工作样本吗? 理想情况下,我希望使用Microsoft nuget库来管理Azure 到目前为止进行了测试: CSManage: =>它在dotnet core上不起作用(它是一个旧的WCF模型) 尝试使用REST: =>我的访问被拒绝。(它只在通过fiddler时起作

我在Azure中有一个工作者角色,我想列出C#dotnet core中的所有实例。 有几个azure管理nuget包。我已经测试了其中一些,但没有一个给出预期的结果。 我还尝试了本地RESTAPI,但找不到可用的示例

有人可以发布工作样本吗? 理想情况下,我希望使用Microsoft nuget库来管理Azure

到目前为止进行了测试: CSManage: =>它在dotnet core上不起作用(它是一个旧的WCF模型)

尝试使用REST: =>我的访问被拒绝。(它只在通过fiddler时起作用,而我没有找到解决方案)

Azure云服务是。所以我们需要使用Azure服务管理API来管理它。如果我们想调用API,我们需要执行X509客户端证书身份验证。有关更多详细信息,请参阅

具体步骤如下

  • 将证书上载到Azure A.创建证书

    $cert = New-SelfSignedCertificate -DnsName yourdomain.cloudapp.net -CertStoreLocation "cert:\LocalMachine\My" -KeyLength 2048 -KeySpec "KeyExchange"
    $password = ConvertTo-SecureString -String "your-password" -Force -AsPlainText
    Export-PfxCertificate -Cert $cert -FilePath ".\my-cert-file.pfx" -Password $password
    Export-Certificate -Type CERT -Cert $cert -FilePath .\my-cert-file.cer
    
    b将
    .cer
    文件上载到Azure(订阅->您的订阅->管理证书)

  • 代码

  • static async Task Main(字符串[]args)
    {
    var_clientHandler=新的HttpClientHandler();
    _clientHandler.ClientCertificates.Add(GetStoreCertificate(“证书的指纹”));
    _clientHandler.ClientCertificateOptions=ClientCertificateOptions.Manual;
    字符串uri=String.Format(“https://management.core.windows.net/{0}/services/hostedservices/{1}?嵌入详细信息=true,“订阅id”,“订阅id”);
    使用(var\u client=newhttpclient(\u clientHandler))
    使用(var request=newhttprequestmessage(HttpMethod.Get,uri)){
    请求。标题。添加(“x-ms-version”、“2014-05-01”);
    添加(“接受”、“应用程序/xml”);
    //添加(“内容类型”、“应用程序/xml”);
    使用(HttpResponseMessage HttpResponseMessage=wait_client.SendAsync(请求)){
    string xmlString=await httpResponseMessage.Content.ReadAsStringAsync();
    Console.WriteLine(httpResponseMessage.StatusCode);
    }
    }
    }
    私有静态X509Certificate2 GetStoreCertificate(字符串指纹)
    {
    X509Store=新的X509Store(“我的”,StoreLocation.LocalMachine);
    尝试
    {
    store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
    X509Certificate2Collection certificates=store.certificates.Find(
    X509FindType.FindByThumbprint,指纹,false);
    if(certificates.Count==1)
    {
    返回证书[0];
    }
    }
    最后
    {
    store.Close();
    }
    抛出新ArgumentException(string.Format(
    “找不到指纹为“{0}”的证书。”,
    指纹);
    }
    
    第二个链接不能与客户id/客户机密一起使用。我相信您将需要实现交互式登录。我用证书取代了身份验证。同样的问题。错误是WWW-Authenticate:Bearer Error=“invalid_token”,Error_description=“JWT token不包含预期的访问群体uri”“management.core.windows.net/”。}。但作用域包含此url。当我把提琴手放在中间时,它就起作用了。我不明白其中的区别
     static async Task Main(string[] args)
     {
       var _clientHandler = new HttpClientHandler();
                    _clientHandler.ClientCertificates.Add(GetStoreCertificate("the cert's thumbprint" ));
                    _clientHandler.ClientCertificateOptions = ClientCertificateOption.Manual;
                    String uri = string.Format("https://management.core.windows.net/{0}/services/hostedservices/{1}?embed-detail=true", "subscription id","<could service name>");
                    using (var _client = new HttpClient(_clientHandler))
                    using (var request = new HttpRequestMessage(HttpMethod.Get, uri)) {
    
                        request.Headers.Add("x-ms-version", "2014-05-01");
                        request.Headers.Add("Accept", "application/xml");
                        //request.Headers.Add("Content-Type", "application/xml");
                        using (HttpResponseMessage httpResponseMessage = await _client.SendAsync(request)) {
                            string xmlString = await httpResponseMessage.Content.ReadAsStringAsync();
                            Console.WriteLine(httpResponseMessage.StatusCode);
                        }
    
                    }
    }
    private static X509Certificate2 GetStoreCertificate(string thumbprint)
            {
    
    
                X509Store store = new X509Store("My", StoreLocation.LocalMachine);
                try
                {
                    store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
                    X509Certificate2Collection certificates = store.Certificates.Find(
                      X509FindType.FindByThumbprint, thumbprint, false);
                    if (certificates.Count == 1)
                    {
                        return certificates[0];
                    }
                }
                finally
                {
                    store.Close();
                }
    
                throw new ArgumentException(string.Format(
                  "A Certificate with Thumbprint '{0}' could not be located.",
                  thumbprint));
            }