【问题标题】:Exception trying to get configuration using Azure .NET SDK尝试使用 Azure .NET SDK 获取配置的异常
【发布时间】:2017-03-20 13:34:14
【问题描述】:

我正在尝试使用 C# .NET SDK 配置我的一个 Web 应用程序。我想使用 Azure AD 应用程序而不是管理证书(我之前已经开始使用)。

我有以下代码:

var subscriptionId = "<subscription-guid>";
var appId = "<app-guid>";
var appKey = "<app-key>";
var tenantId = "<tenant-guid>";

var context = new AuthenticationContext("https://login.windows.net/" + tenantId);
ClientCredential clientCredential = new ClientCredential(appId, appKey);
var tokenResponse = context.AcquireTokenAsync("https://management.core.windows.net/", clientCredential).Result;
var accessToken = tokenResponse.AccessToken;

var myWebspace = "<my-webspace>";
var myWebsite = "<my-website>";

var client = new WebSiteManagementClient(new TokenCloudCredentials(subscriptionId, accessToken));
var config = client.WebSites.GetConfigurationAsync(myWebspace, myWebsite).Result;

...但它在最后一行抛出以下错误:

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

可能出了什么问题?我已创建应用程序并授予它以下权限:

Windows Azure 服务管理

应用程序权限:0

委派权限:1

以组织用户身份访问 Azure 服务管理(预览版)

Windows Azure 活动目录

应用程序权限:0

委派权限:1

登录并阅读用户资料

提前致谢

克里斯

【问题讨论】:

标签: c# .net azure azure-active-directory azure-sdk-.net


【解决方案1】:

正如 Gaurav 在另一个 SO thread 中提到的那样。我们可以切换到 Azure 资源管理器 API。另一个重要的事情是我们还需要创建服务主体来访问资源。我们可以使用 azure PowerShell 命令轻松完成。有关如何在 Azure AD 中注册 Web 应用和创建服务主体的更多详细步骤,请参阅article

New-AzureRmRoleAssignment -RoleDefinitionName Contributor -ServicePrincipalName $app.ApplicationId.Guid

Microsoft.Azure.Management.WebSites SDK 实现 WebApp 资源管理 API。这是一个预发布版本。 以下是我的代码示例:

 var subscriptionId = "Your subscription Id";
 var appId = "Application Id";
 var appKey = "secret key";
 var tenantId = "tenant id";
 var serviceCreds = ApplicationTokenProvider.LoginSilentAsync(tenantId, appId, appKey).Result;
 var webClient = new WebSiteManagementClient(serviceCreds) { SubscriptionId = subscriptionId };
 var result = webClient.Sites.GetSiteConfigWithHttpMessagesAsync("ResourceGroup Name", "Web App name").Result;

打包文件:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Microsoft.Azure.Management.Websites" version="1.3.2-preview" targetFramework="net452" />
  <package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="2.28.1" targetFramework="net452" />
  <package id="Microsoft.Rest.ClientRuntime" version="2.3.1" targetFramework="net452" />
  <package id="Microsoft.Rest.ClientRuntime.Azure" version="3.1.0" targetFramework="net452" />
  <package id="Microsoft.Rest.ClientRuntime.Azure.Authentication" version="2.2.4.1-preview" targetFramework="net452" />
  <package id="Newtonsoft.Json" version="6.0.8" targetFramework="net452" />
</packages>

演示测试结果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-13
    • 2020-02-14
    • 1970-01-01
    • 2019-08-02
    • 2019-08-02
    • 1970-01-01
    • 1970-01-01
    • 2021-02-25
    相关资源
    最近更新 更多