我正在尝试使用 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;
}
}
关于c# - Microsoft.Azure.Management.Api管理实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52108663/