我需要来自订阅ID和身份验证令牌的Azure存储列表。 我按照参考-`
进行编码https://msdn.microsoft.com/en-us/library/azure/ee460787.aspx?f=255&MSPPError=-2147217396
但我无法获取该数据,作为回应,我只获得代码401 Unauthorized access
我已尝试使用c#中的代码 -
获取授权令牌 -
private static string GetAuthorizationToken()
{
ClientCredential cc = new ClientCredential(ClientId, ServicePrincipalPassword);
var context = new AuthenticationContext("https://login.windows.net/" + AzureTenantId);
var result = context.AcquireTokenAsync("https://management.azure.com/", cc);
if (result == null)
{
throw new InvalidOperationException("Failed to obtain the JWT token");
}
return result.Result.AccessToken;
}
然后
AuthToken = GetAuthorizationToken();
TokenCredentials = new TokenCredentials(AuthToken);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://management.core.windows.net/<My Subscription ID>/services/storageservices");
request.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + AuthToken);
request.ContentType = "application/json";
request.Method = "GET";
//request.Headers["Authorization"] = "Bearer " + AuthToken; // Also tried this
request.Headers["x-ms-version"] = "2016-05-31";//
//https://management.core.windows.net/<subscription-id>/services/storageservices
//header - "x-ms-version"
HttpWebResponse response = null;
try
{
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
//ex.Message;
}
请建议从订阅ID和身份验证令牌获取存储帐户列表的任何解决方案。如果你有其他更好的解决方案,那么这个获取存储列表然后也请建议。
答案 0 :(得分:0)
我使用以下函数创建类似的请求:
private static async Task<HttpWebRequest> createHttpRequestWithToken(Uri uri)
{
HttpWebRequest newRequest = (HttpWebRequest)HttpWebRequest.Create(uri);
string clientId = ConfigurationManager.AppSettings["ClientId"]);
string clientSecret = ConfigurationManager.AppSettings["ClientSecret"]);
ClientCredential creds = new ClientCredential(clientId, clientSecret);
AuthenticationContext authContext = new AuthenticationContext(string.Format("https://login.microsoftonline.com/{0}/", ConfigurationManager.AppSettings["OrganizationId"]));
AuthenticationResult authResult = await authContext.AcquireTokenAsync("https://management.core.windows.net/", creds);
newRequest.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + authResult.AccessToken);
return newRequest;
}
并创建这样的请求:
HttpWebRequest request = createHttpRequestWithToken(uri).Result;
此外,需要授予Windows Azure Service Management API访问资源的权限。这可以在AD Service Principal中的Configure选项卡中完成。
答案 1 :(得分:0)
要list all the storage accounts available under the subscription,您可以尝试使用:
GET / subscriptions / {subscriptionId} /providers/Microsoft.Storage/storageAccounts?api-version=2016-01-01
以下示例代码可以正常使用,请参阅。
string tenantId = "xxxx";
string clientId = "xxxx";
string clientSecret = "xxxx";
string subscriptionid = "xxxx";
string authContextURL = "https://login.windows.net/" + tenantId;
var authenticationContext = new AuthenticationContext(authContextURL);
var credential = new ClientCredential(clientId, clientSecret);
var result = await authenticationContext.AcquireTokenAsync(resource: "https://management.azure.com/", clientCredential: credential);
if (result == null)
{
throw new InvalidOperationException("Failed to obtain the JWT token");
}
string token = result.AccessToken;
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(string.Format("https://management.azure.com/subscriptions/{0}/providers/Microsoft.Storage/storageAccounts?api-version=2016-01-01", subscriptionid));
request.Method = "GET";
request.Headers["Authorization"] = "Bearer " + token;
HttpWebResponse response = null;
try
{
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
//ex.Message;
}
fiddler的回应看起来像这样。
此外,请确保assign the application to a role,如果没有将应用程序分配给某个角色,则会出现403错误。