【问题标题】:Get list of instances in hosted services in Azure获取 Azure 中托管服务中的实例列表
【发布时间】:2020-05-25 14:36:32
【问题描述】:

我在 Azure 中有一个辅助角色,我想列出 C# dotnet core 中的所有实例。 有几个 azure 管理 nuget 包。我已经测试了其中一些,但没有一个给出预期的结果。 我也尝试了原生 rest api,但找不到工作示例。

有人可以发布工作示例吗? 理想情况下,我想使用 Microsoft nuget 库来管理 Azure。

到目前为止测试: 客服管理:https://github.com/Plasma/csmanage => 它不适用于 dotnet core(它是一个旧的 WCF 模型)。

尝试使用 REST:401 unauthorized Azure management api => 我被拒绝访问。 (只有通过fiddler才有效,我没有找到解决方案)

【问题讨论】:

  • 第二个链接不适用于客户端 ID/客户端密码。我相信你需要实现交互式登录。
  • 我用证书代替了身份验证。同样的问题。错误是 WWW-Authenticate: Bearer error="invalid_token", error_description="JWT 令牌不包含预期的受众 uri 'management.core.windows.net/'。" }。但范围包含此 url。当我把提琴手放在中间时,它就起作用了。我不明白区别

标签: azure .net-core azure-cloud-services


【解决方案1】:

Azure 云服务是Azure classic resource。所以我们需要使用 Azure 服务管理 API 来管理它。如果我们要调用API,我们需要做X509客户端证书认证。更多详情请参考document

具体步骤如下

  1. 上传证书到 Azure 一种。创建证书

    $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(订阅 -> 您的订阅 -> 管理证书)

  2. 代码

 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));
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-17
    • 2014-01-03
    • 1970-01-01
    • 1970-01-01
    • 2017-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多