c# - Microsoft.Azure.Management.Api管理实现

标签 c# azure azure-api-management

我正在尝试使用 Microsoft.Azure.Management.ApiManagement 4.0.4-preview 实现 Azure API 管理 API。

我没有在哪里看到实现文档。我尝试了下面的代码。但我收到身份验证错误。

Microsoft.Rest.Azure.CloudException: 'Authentication failed. The 'Authorization' header is provided in an invalid format.'

BasicAuthenticationCredentials basicAuthenticationCredentials = new BasicAuthenticationCredentials();
basicAuthenticationCredentials.UserName = "**********";
basicAuthenticationCredentials.Password = "*******";

var token = "Bearer **********"; // copied bear token from https://learn.microsoft.com/en-us/rest/api/apimanagement/user/get by logging proper user name and password

 ApiManagementClient apiManagementClient = new ApiManagementClient(basicAuthenticationCredentials);
 apiManagementClient.SubscriptionId = "*************************************";           
 apiManagementClient.HttpClient.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", token);
 apiManagementClient.ApiManagementService.Get("resourcegroupname", "POCAPIManagementService"); // error happening from this line

 var user = apiManagementClient.User.Get("resourcegroupname", "POCAPIManagementService", "1");

最佳答案

经过两周的努力,我们找到了 Microsoft.Azure.Management.ApiManagement 的方法 dll 实现。

1) 在 azure ad 中创建应用程序 2) 转到 APIM => 访问控制 (IAM) 选项卡 3) 添加上面创建的应用程序(需要 APIM 中的权限才能执行此操作) 4) 现在您应该能够在 APIM 访问控制 (IAM) 选项卡中看到 Azure AD 应用程序

这将为您在 Azure AD 中创建的应用程序提供委派权限

我们可以使用客户端凭据流来获取针对 Azure AD 的委派访问 token 。 使用范围为 https://management.azure.com

下面给出了用于实现 Microsoft.Azure.Management.ApiManagement dll 的客户端凭据流程的示例代码。

public class myServiceCredentials : ServiceClientCredentials{
private string AuthenticationToken { get; set; }
public override void InitializeServiceClient<T>(ServiceClient<T> client)
    {
        var authenticationContext = new 
   AuthenticationContext("https://login.windows.net/{tenantID}");
        var credential = new ClientCredential(clientId: "xxxxx-xxxx-xx-xxxx-xxx", 
  clientSecret: "{clientSecret}");
        var result = authenticationContext.AcquireToken(resource: 
        "https://management.core.windows.net/", clientCredential: credential);

        if (result == null)
        {
            throw new InvalidOperationException("Failed to obtain the JWT token");
        }
        AuthenticationToken = result.AccessToken;
    }
}

谢谢https://github.com/Azure/azure-sdk-for-net/issues/4727

关于c# - Microsoft.Azure.Management.Api管理实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52108663/

相关文章:

asp.net-mvc - ASPNETCORE 在 VS 2015 中本地工作,但发布到 Azure 时出现错误 500

azure - 我们如何在使用多个 APIM 服务实现高可用性的同时,通过 Azure API 管理服务实现全局速率限制?

azure - API 管理的默认版本

c# - 从数据表添加到字典记录

c# - 集群环境中的 WCF 双工通信

c# - 如何去除 ToolStrip Winforms 控件一角的这种奇怪的视觉伪像?

c# - 使用 C# 将 DateTime 插入 SQL Server

Azure 数据资源管理器查询从 IP 地址查找国家/地区

azure - 检测拉取请求所请求的构建以及 PR 的任何更新所运行的构建

azure - 检查 Azure API 管理中的 Traceparent http header 的正确性