【发布时间】:2016-03-12 20:17:23
【问题描述】:
我希望从将托管在 Azure Web 应用程序上的 c# 应用程序启动 Azure Runbook。
我正在使用证书身份验证(只是为了测试我是否可以连接并检索一些数据)
到目前为止,这是我的代码:
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 库(如身份验证、数据中心等),但我得到了同样的错误:
ForbiddenError: The server failed to authenticate the request. Verify that the certificate is valid and is associated with this subscription.
我确定它只是 1 个复选框,我需要在那个单一的管理门户中的某个地方打勾,但我已经遵循了一些关于如何执行此操作的教程,但最终都出现了这个错误......
【问题讨论】:
-
我一直在使用
ClientCredential而不是UserCredentialcloudidentity.com/blog/2014/07/08/… -
@MilenPavlov 对此表示感谢,但我已经尝试使用本地应用程序和 Web 应用程序,同时使用客户端凭据(使用密钥)和使用用户名/密码的用户凭据。我遇到了同样的问题(我成功获得了一个令牌,它只是不允许我执行任何操作)
-
我在
var content = await client.Runbooks.ListByNameAsync("automationAccount", "runbookName", cancelationToken);上找不到异常 -
@MilenPavlov 你和我有同样的问题吗?
-
在这里找到它stackoverflow.com/questions/24999518/…,刚刚测试过:)
标签: c# azure azure-automation