Windows Azure Development:禁止错误:服务器无法对请求进行身份验证
本文关键字:请求 身份验证 服务器 Development Azure 禁止 错误 Windows | 更新日期: 2023-09-27 18:28:16
最近,我遇到一个Azure身份验证问题,错误消息如下:附加信息:禁止错误:服务器无法对请求进行身份验证。请验证证书是否有效以及是否与此订阅关联。
这是代码:
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();
var computeClient=新Microsoft.WindowsAzure.Management.Computer.ComputeManagementClient(凭据,新Uri("https://management.core.chinacloudapi.cn/"));
默认Uri为:https://management.core.windows.net因此,中国Azure需要更改为:***/management.core.chinaclodapi.cn/