[英]How to manage Azure API management service in my C# application
我想使用C#管理我的Azure API管理服務。 根據我的研究,Azure .Net sdk提供了Microsoft.azure Management.ApiManagemnt軟件包來管理APIM。 但是我不知道如何實現ApiManageClient類。 有人可以提供一些建議嗎?
Microsoft.Rest.Azure.CloudException:'身份驗證失敗。
BasicAuthenticationCredentials basicAuthenticationCredentials = new BasicAuthenticationCredentials();
basicAuthenticationCredentials.UserName = "**********";
basicAuthenticationCredentials.Password = "*******";
ApiManagementClient apiManagementClient = new ApiManagementClient(basicAuthenticationCredentials);
apiManagementClient.SubscriptionId = "*************************************";
apiManagementClient.ApiManagementService.Get("resourcegroupname", "POCAPIManagementService");
根據我的測試,您可以使用piManagementClient.HttpClient.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", token);
添加Authorization標頭piManagementClient.HttpClient.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", token);
例如:
使用Azure PowerShell創建Azure服務主體
$credentials = New-Object Microsoft.Azure.Commands.ActiveDirectory.PSADPasswordCredential -Property @{ StartDate=Get-Date; EndDate=Get-Date -Year 2024; Password=<Choose a strong password>}
$sp = New-AzAdServicePrincipal -DisplayName ServicePrincipalName -PasswordCredential $credentials
管理服務主體角色
New-AzRoleAssignment -ApplicationId <service principal application ID> -RoleDefinitionName "Contributor"
碼
// use Microsoft.IdentityModel.Clients.ActiveDirectory to get access token. The access token can be used to manage Azure resource
string tenantId = "your tenant id or name, for example: hanxia.onmicrosoft.com";
string clientId = "1950a258-227b-4e31-a9cf-717495945fc2"; // it is a public client for every tenant.
string resource = "https://management.core.windows.net/";
string clientSecret = "";
string password = "";
ClientCredentia clientCredentia = new ClientCredentia(clientId,clientSecret);
var context = new AuthenticationContext("https://login.microsoftonline.com/" + tenantId);
AuthenticationResult result = context.AcquireTokenAsync(resource, clientCredentia).Result;
var token = "Bearer " +result.AccessToken;
ApiManagementClient apiManagementClient = new ApiManagementClient(basicAuthenticationCredentials);
apiManagementClient.SubscriptionId = "*************************************";
apiManagementClient.HttpClient.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", token);
apiManagementClient.ApiManagementService.Get("resourcegroupname", "POCAPIManagementService");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.