Azure SqlManagementClient - 禁止使用TokenCloudCredentials

时间:2017-09-28 15:49:22

标签: c# azure azure-sql-database azure-management-api

我正在尝试使用azure连接到我的SQL服务器并从.net应用程序列出dbs,但我一直在

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

即使我正在尝试使用TokenCloudCredentials的Sql Management客户端。

var authContext = new AuthenticationContext(authority);
var clientCredential = new ClientCredential(clientId, appKey);
var result = authContext.AcquireTokenAsync(resource, clientCredential).Result;
var credentials = new Microsoft.Azure.TokenCloudCredentials(subscriptionId, result.AccessToken);
var client = new SqlManagementClient(credentials);
try
    {
        var servers = await client.Servers.ListAsync();
    }
    catch (CloudException c)
    {
        Console.WriteLine(c.Message);
        throw;
    }

AD应用程序具有访问资源组和Azure Management API的权限。 任何想法,为什么它一直抱怨证书,同时使用令牌?

编辑:我设法使用“新的”流畅管理API。您需要创建与订阅关联的AD应用程序,并且可以访问资源组。然后只需创建凭据并初始化流畅的API。

using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent.Authentication;
using Microsoft.Azure.Management.ResourceManager.Fluent.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AzureManagement
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var azureCredentials = new AzureCredentials(new
                ServicePrincipalLoginInformation
            {
                ClientId = "clientId",
                ClientSecret = "clientSecret="
            }, "tenantId", AzureEnvironment.AzureGlobalCloud);

            var _azure = Azure
           .Configure()
           .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
           .Authenticate(azureCredentials)
           .WithSubscription("subscriptionId");

            var sql = _azure.SqlServers.List().ToList();
            foreach (var s in sql)
            {
                var dbs = s.Databases.List().ToList();
            }
            Console.ReadLine();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我不确定这是否得到支持。请为此问题创建一个支持案例。