最近,我面临一个Azure身份验证问题,错误消息如下: 其他信息:ForbiddenError:服务器无法验证请求。验证证书是否有效并与此订阅相关联。
这是代码:
private TokenCloudCredentials credentials;
public ManagerClient(TokenCloudCredentials credentials)
{
this.credentials = credentials;
}
public void GetHostedServices()
{
var computeClient = new Microsoft.WindowsAzure.Management.Compute.ComputeManagementClient(credentials);
var services = computeClient.HostedServices.List().HostedServices;
foreach (var service in services)
{
Console.WriteLine("ServiceName: " + service.ServiceName);
Console.WriteLine("Uri: " + service.Uri);
Console.WriteLine();
}
}
public static TokenCloudCredentials GetCredentials(string subscriptionId = "")
{
var token = GetAccessToken();
if(string.IsNullOrEmpty(subscriptionId))
subscriptionId = ConfigurationManager.AppSettings["subscriptionId"];
var credential = new TokenCloudCredentials(subscriptionId, token);
return credential;
}
private static string GetAccessToken()
{
AuthenticationResult result = null;
var context = new AuthenticationContext(string.Format(
ConfigurationManager.AppSettings["login"],
ConfigurationManager.AppSettings["tenantId"]));
result = context.AcquireToken(
ConfigurationManager.AppSettings["apiEndpoint"],
ConfigurationManager.AppSettings["clientId"],
new Uri(ConfigurationManager.AppSettings["redirectUri"]));
if (result == null)
{
throw new InvalidOperationException("Failed to obtain the JWT token");
}
return result.AccessToken;
}
var credentials = Authorizator.GetCredentials("37a80965-5107-4f9b-91c6-a1198ee40226");
new ManagerClient(credentials).GetHostedServices();
答案 0 :(得分:0)
var computeClient = new Microsoft.WindowsAzure.Management.Compute.ComputeManagementClient(credentials,new Uri(“https://management.core.chinacloudapi.cn/”));
默认Uri是:https://management.core.windows.net 所以China Azure需要改为:*** / management.core.chinacloudapi.cn /