简体   繁体   English

如何使用HTTP触发的Azure功能删除Azure存储帐户

[英]How to delete Azure Storage Account with a HTTP triggered Azure Function

Having trouble deleting a storage account with an azure function. 删除具有azure功能的存储帐户时出错。 If anybody could lead me the right way it would be greatly appreciated 如果有人能以正确的方式引导我,我将不胜感激

Expected result: When I make a POST request to my Azure Function, then my Azure Function should delete a Storage Account in a different Resource group 预期结果:当我向Azure功能发出POST请求时,我的Azure功能应删除其他资源组中的存储帐户

Edit: This is what I have so far, after Peter Pan's help: 编辑:这是我在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();

}

Error being returned: 返回错误:

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

It sounds like you want to use the REST API Storage Accounts - Delete to delete a Storage Account in Azure Function with Http Trigger, but there is missing some codes to add the header Authorization into your http delete request. 听起来您希望使用REST API Storage Accounts - Delete以使用Http Trigger删除Azure功能中的存储帐户,但是缺少一些代码将标题Authorization添加到您的http删除请求中。

The request of Storage Accounts - Delete REST API should be like below. 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>

The code to call the api above should be as 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();

You can refer to the source code TodoListController.cs of the code sample Azure-Samples/active-directory-dotnet-webapp-webapi-oauth2-appidentity to know more details about how to get the access token from Azure AD by your registered web client. 您可以参考代码示例Azure-Samples/active-directory-dotnet-webapp-webapi-oauth2-appidentity的源代码TodoListController.cs ,以了解有关如何通过注册的Web客户端从Azure AD获取访问令牌的更多详细信息。 And for more details about client register in Azure AD, you can follow the offical document Azure REST API Reference to do it and grant the necessary permission or role on Azure portal. 有关Azure AD中的客户端注册的更多详细信息,您可以按照Azure REST API Reference的官方文档执行此操作,并在Azure门户上授予必要的权限或角色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM