Microsoft.Azure.Management.Api管理实施 [英] Microsoft.Azure.Management.ApiManagement Implementation
问题描述
我正在尝试使用Microsoft.Azure.Management.ApiManagement
4.0.4-preview实现Azure API管理API.
I am trying to implement Azure API Management APIs using Microsoft.Azure.Management.ApiManagement
4.0.4-preview.
没有,我在哪里看到实现文档.我尝试了下面的代码.但是我收到验证错误.
No where I see documentation for implementation. I tried below code. but I am getting authentication error.
Microsoft.Rest.Azure.CloudException:'身份验证失败. 'Authorization'标头以无效格式提供.'
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://docs.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实施.
After two weeks struggle we found way to Microsoft.Azure.Management.ApiManagement dll Implementation.
1)在Azure广告中创建应用程序 2)转到您的APIM =>访问控制(IAM)选项卡 3)添加上面创建的应用程序(需要在APIM中执行此操作) 4)现在您应该可以在APIM访问控制(IAM)选项卡中看到Azure AD应用程序
1) Create application inside azure ad 2) Go to your APIM => Access control (IAM) Tab 3) Add the above created application (permission is required to do this in APIM) 4) Now you should be able to see Azure AD application in APIM Access control (IAM) Tab
这将为在Azure AD中创建的应用程序提供委派权限
This will provide delegated permission to your application which is created in Azure AD
我们可以使用客户端凭据流来获取针对Azure AD的委派访问令牌. 将范围用作 https://management.azure.com
We can use client credential flow to get delegated access token against Azure AD. Use scope as https://management.azure.com
下面给出了用于为Microsoft.Azure.Management.ApiManagement dll实现客户端凭据流的示例代码.
The sample code for implementing client credential flow for Microsoft.Azure.Management.ApiManagement dll is given below.
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
这篇关于Microsoft.Azure.Management.Api管理实施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!