简体   繁体   English

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

[英]Error when calling any method on Service Management API

I'm looking to start an Azure runbook from ac# application which will be hosted on an Azure web app. 我希望从ac#应用程序启动Azure运行手册,该应用程序将托管在Azure Web应用程序上。

I'm using certificate authentication (in an attempt just to test that I can connect and retrieve some data) 我正在使用证书身份验证(试图只是为了测试我是否可以连接和检索某些数据)

Here's my code so far: 到目前为止,这是我的代码:

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

Every time I run this, no matter what certificate I use I get the same error: 每次运行此命令时,无论我使用什么证书,都会遇到相同的错误:

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.

I've tried downloading the settings file which contains the automatically generated management certificate you get when you spin up the Azure account... nothing I do will let me talk to any of the Azure subscription 我尝试下载包含您在启动Azure帐户时获得的自动生成的管理证书的设置文件...我什么也不会让我与任何Azure订阅进行交流

Am I missing something fundamental here? 我在这里缺少基本的东西吗?

Edit: some additional info... 编辑:一些其他信息...

So I decided to create an application and use the JWT authentication method. 因此,我决定创建一个应用程序并使用JWT身份验证方法。

I've added an application, given the application permissions to the Azure Service Management API and ensured the user is a co-administrator and I still get the same error, even with the token... 我已经添加了一个应用程序,并授予了该应用程序对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>");

I've also tried using other Azure libs (like auth, datacentre etc) and I get the same error: 我也尝试使用其他Azure库(例如auth,datacentre等),但遇到相同的错误:

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

I'm sure it's just 1 tickbox I need to tick buried somewhere in that monolithic Management Portal but I've followed a few tutorials on how to do this and they all end up with this error... 我确定这只是1个复选框,我需要在整体式管理门户中的某个地方打钩,但我已经按照一些教程进行了操作,最终都遇到了这个错误...

Ensure that your Management certificate has private key and was not made from the .CER file. 确保您的管理证书具有私钥,并且不是由.CER文件制成的。 The fact that you're not supplying a password when generating the X509Certificate object makes me think you're using public key only 您在生成X509Certificate对象时未提供密码的事实使我认为您仅在使用公用密钥

Ensure that your Managemnet's certificate public key (.CER file) has been uploaded to the Azure management portal (legacy version, Management Certificate area) 确保您的Managemnet的证书公钥(.CER文件)已上传到Azure管理门户(旧版,“管理证书”区域)

Use CertificateCloudCredentials and not any other credential type of an object 使用CertificateCloudCredentials而不是对象的任何其他凭据类型

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

Also in portal: 1) Application is multitenant 2) Permissions to other applications section - Windows Azure Service Manager - Delegated permissions "Access Azure Service Management(preview)" 也在门户中:1)应用程序是多租户2)对其他应用程序的权限部分-Windows Azure服务管理器-委派的权限“访问Azure服务管理(预览)”

Ok, stupid really but one of the tutorials I followed suggested installing the prerelease version of the libs. 好的,这确实很愚蠢,但是我遵循的教程之一建议安装lib的预发行版本。

Installing the preview (0.15.2-preview) has fixed the issue! 安装预览版(0.15.2-preview)已解决此问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM