删除具有天蓝色功能的存储帐户时遇到麻烦。如果有人能以正确的方式带领我,将不胜感激
预期结果: 当我向Azure函数发出POST请求时,Azure函数应删除其他资源组中的存储帐户
编辑:这是我在彼得潘(Peter Pan)的帮助下到目前为止所拥有的:
#r "Newtonsoft.Json"
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Claims;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
// string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
// dynamic data = JsonConvert.DeserializeObject(requestBody);
// performing something with Json here
// ...
AuthenticationResult result = null;
//.... codes for getting access token
AuthenticationContext authContext = new AuthenticationContext(authority);
ClientCredential clientCredential = new ClientCredential("xxxx-xxxx-xxxx-xxxx-xxxx", "xxxxxxxxxxxxxxxxxxx/=");
result = await authContext.AcquireTokenAsync("/subscriptions/xxxxx/resourceGroups/xxxx", clientCredential);
var client = new HttpClient();
client.BaseAddress = new Uri("https://management.azure.com/");
client.DefaultRequestHeaders.Add("Authorization", "Bearer "+result.AccessToken);
var resp = client.DeleteAsync("subscriptions/xxxxxxx/resourceGroups/xxxxxx/providers/Microsoft.Storage/storageAccounts/xxxxxxxxxx?api-version=2018-11-01");
return resp.StatusCode.Equals("200") ? new OkResult() : new NotFoundResult();
}
返回错误:
2019-04-13T10:23:51.234 [Error] run.csx(18,17): error CS0234: The type or namespace name 'IdentityModel' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)
2019-04-13T10:23:51.412 [Error] run.csx(32,1): error CS0246: The type or namespace name 'AuthenticationResult' could not be found (are you missing a using directive or an assembly reference?)
2019-04-13T10:23:51.454 [Error] run.csx(34,1): error CS0246: The type or namespace name 'AuthenticationContext' could not be found (are you missing a using directive or an assembly reference?)
2019-04-13T10:23:51.514 [Error] run.csx(34,41): error CS0246: The type or namespace name 'AuthenticationContext' could not be found (are you missing a using directive or an assembly reference?)
2019-04-13T10:23:51.556 [Error] run.csx(34,63): error CS0103: The name 'authority' does not exist in the current context
答案 0 :(得分:0)
听起来您想使用REST API Storage Accounts - Delete
来删除带有Http触发器的Azure Function中的存储帐户,但是缺少一些代码将标头Authorization
添加到您的http删除请求中
Storage Accounts - Delete
REST API的请求应如下所示。
DELETE https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{storageAccountName}?api-version=2018-11-01
Authorization: Bearer <accessToken got from Azure AD by your web client as the code `result.AccessToken` below>
调用上面的api的代码应该如下。
AuthenticationResult result = null;
//.... codes for getting access token
AuthenticationContext authContext = new AuthenticationContext(authority);
ClientCredential clientCredential = new ClientCredential(clientId, appKey);
result = await authContext.AcquireTokenAsync(todoListResourceId, clientCredential);
var client = new HttpClient();
client.BaseAddress = new Uri("https://management.azure.com/")
client.DefaultRequestHeaders.Add("Authorization", "Bearer "+result.AccessToken);
var resp = client.DeleteAsync("subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{storageAccountName}?api-version=2018-11-01")
return resp.StatusCode.Equals("200") ? new OkResult() : new NotFoundResult();
您可以参考代码示例TodoListController.cs
的源代码Azure-Samples/active-directory-dotnet-webapp-webapi-oauth2-appidentity
,以了解有关如何通过已注册的Web客户端从Azure AD获取访问令牌的更多详细信息。有关在Azure AD中进行客户端注册的更多详细信息,可以按照官方文档Azure REST API Reference
进行操作,并在Azure门户上授予必要的权限或角色。