[英]How to delete Azure Storage Account with a HTTP triggered Azure Function
删除具有azure功能的存储帐户时出错。 如果有人能以正确的方式引导我,我将不胜感激
预期结果:当我向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
听起来您希望使用REST API Storage Accounts - Delete
以使用Http Trigger删除Azure功能中的存储帐户,但是缺少一些代码将标题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();
您可以参考代码示例Azure-Samples/active-directory-dotnet-webapp-webapi-oauth2-appidentity
的源代码TodoListController.cs
,以了解有关如何通过注册的Web客户端从Azure AD获取访问令牌的更多详细信息。 有关Azure AD中的客户端注册的更多详细信息,您可以按照Azure REST API Reference
的官方文档执行此操作,并在Azure门户上授予必要的权限或角色。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.