在Service Management API上调用任何方法时出错

时间:2015-12-08 02:28:38

标签: c# azure azure-automation

我希望从c#应用程序启动Azure Runbook,该应用程序将托管在Azure Web应用程序上。

我使用证书身份验证(试图测试我可以连接并检索一些数据)

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

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 Service Management 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,数据中心等),我也遇到了同样的错误:

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

我确定它只是一个复选框我需要在那个单一的管理门户网站的某个地方刻录,但是我已经按照一些教程来了解如何做到这一点,他们都得到了这个错误...

3 个答案:

答案 0 :(得分:1)

确保您的管理证书具有私钥,而不是来自.CER文件。您在生成X509Certificate对象时未提供密码的事实让我认为您只使用公钥

确保您的Managemnet证书公钥(.CER文件)已上载到Azure管理门户(旧版本,管理证书区域)

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

答案 1 :(得分:1)

    public async Task StartAzureRunbook()
    {
        try
        {
            var subscriptionId = "azure subscription Id";
            string base64cer = "****long string here****"; //taken from http://stackoverflow.com/questions/24999518/azure-api-the-server-failed-to-authenticate-the-request

            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 Service Manager - 委派权限&#34;访问Azure服务管理(预览)&#34;

答案 2 :(得分:1)

好的,真的很蠢,但我遵循的其中一个教程建议安装libs的预发布版本。

安装预览(0.15.2预览版)解决了问题!