在服务管理 API 上调用任何方法时出错

Error when calling any method on Service Management API

我希望从将托管在 Azure Web 应用程序上的 c# 应用程序开始一本 Azure 运行book。

我正在使用证书身份验证(只是为了测试我是否可以连接并检索一些数据)

到目前为止,这是我的代码:

var cert = ConfigurationManager.AppSettings["mgmtCertificate"];

var creds = new Microsoft.Azure.CertificateCloudCredentials("<my-sub-id>",
new X509Certificate2(Convert.FromBase64String(cert)));

var client = new Microsoft.Azure.Management.Automation.AutomationManagementClient(creds, new Uri("https://management.core.windows.net/"));
var content = client.Runbooks.List("<resource-group-id>", "<automation-account-name>");

每次我 运行 这个,无论我使用什么证书,我都会得到同样的错误:

An unhandled exception of type 'Hyak.Common.CloudException' occurred in Microsoft.Threading.Tasks.dll

Additional information: ForbiddenError: The server failed to authenticate the request. Verify that the certificate is valid and is associated with this subscription.

我已经尝试下载设置文件,其中包含您在启动 Azure 帐户时获得的自动生成的管理证书...我做的任何事情都不会让我与任何 Azure 订阅对话

我是不是漏掉了一些基本的东西?

编辑:一些附加信息...

所以我决定创建一个应用程序并使用 JWT 身份验证方法。

我已经添加了一个应用程序,赋予应用程序对 Azure 服务管理的权限 API 并确保用户是共同管理员,但我仍然遇到同样的错误,即使使用令牌...

const string tenantId = "xx";
const string clientId = "xx";

var context = new AuthenticationContext(string.Format("https://login.windows.net/{0}", tenantId));

var user = "<user>";
var pwd = "<pass>";
var userCred = new UserCredential(user, pwd);

var result = context.AcquireToken("https://management.core.windows.net/", clientId, userCred);

var token = result.CreateAuthorizationHeader().Substring("Bearer ".Length);  // Token comes back fine and I can inspect and see that it's valid for 1 hour - all looks ok...

var sub = "<subscription-id>";
var creds = new TokenCloudCredentials(sub, token);
var client = new AutomationManagementClient(creds, new Uri("https://management.core.windows.net/"));

var content = client.Runbooks.List("<resource-group>", "<automation-id>");

我也尝试过使用其他 Azure 库(如 auth、datacentre 等),但我遇到了同样的错误:

ForbiddenError: The server failed to authenticate the request. Verify that the certificate is valid and is associated with this subscription.

我确定它只是 1 个复选框,我需要在那个单一的管理门户中的某个地方打勾,但我已经按照一些教程学习如何做到这一点,但他们都以这个错误告终...

确保您的管理证书具有私钥并且不是从 .CER 文件生成的。您在生成 X509Certificate 对象时没有提供密码这一事实让我认为您只使用 public 密钥

确保你的Managemnet的证书public密钥(.CER文件)已经上传到Azure管理门户(旧版,管理证书区)

使用 CertificateCloudCredentials 而不是对象的任何其他凭证类型

    public async Task StartAzureRunbook()
    {
        try
        {
            var subscriptionId = "azure subscription Id";
            string base64cer = "****long string here****"; //taken from 

            var cert = new X509Certificate2(Convert.FromBase64String(base64cer));

            var client = new Microsoft.Azure.Management.Automation.AutomationManagementClient(new CertificateCloudCredentials(subscriptionId, cert));
            var ct = new CancellationToken();

            var content = await client.Runbooks.ListByNameAsync("MyAutomationAccountName", "MyRunbookName", ct);


            var firstOrDefault = content?.Runbooks.FirstOrDefault();
            if (firstOrDefault != null)
            {
                var operation = client.Runbooks.Start("MyAutomationAccountName", new RunbookStartParameters(firstOrDefault.Id));
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }       

也在门户中: 1)应用程序是多租户的 2) 对其他应用程序的权限部分 - Windows Azure 服务管理器 - 委派权限 "Access Azure Service Management(preview)"

好吧,确实很愚蠢,但我遵循的其中一个教程建议安装库的预发布版本。

安装预览版 (0.15.2-preview) 已解决问题![​​=10=]