gpt4 book ai didi

c# - 有没有办法使用 C# 从我的 Function App 中删除 Azure Function?

转载 作者:行者123 更新时间:2023-12-03 01:36:44 25 4
gpt4 key购买 nike

我正在尝试通过 C# 从我的函数应用程序中删除 Azure 函数。但是,在以编程方式删除它时,用户界面上看不到该函数,但是当我通过高级工具(Kudu)检查它时,我仍然可以看到我的Azure函数。

基本上,在删除 Azure 函数时,我所做的就是删除它的 function.json,这样 Azure 函数在 Functions App 列表中就不再可见(见下图)

enter image description here

但是当我去Advanced Kudu检查它是否被删除时,我仍然可以看到它,但是没有function.json文件。我以前做过这个(大约 6 个月前),当时它工作正常。我不知道是我做错了还是有什么改变。

enter image description here

任何有关代码的帮助将不胜感激。

谢谢

编辑:

我掌握的详细信息是 Function App 的用户名、密码、URL、名称 ( https://my-function-app.scm.azurewebsites.net/api/vfs/site/wwwroot ) 和 azure 函数的名称。

我在 6 个月前所做的一些示例代码

private WebClient _webClient = new WebClient
{
Headers = { ["ContentType"] = "application/json" },
Credentials = new NetworkCredential(username, password),
BaseAddress = functionsSiteRoot,
};

var functionJson =
JsonConvert.DeserializeObject<FunctionSettings>(_webClient.DownloadString("MyFunctionName/function.json"));

_webClient.Headers["If-Match"] = "*";
_webClient.UploadString("MyFunctionName/function.json", "DELETE", JsonConvert.SerializeObject(functionJson));

最佳答案

您可以使用 REST API 来执行此操作。

https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Web/sites/{name}/functions/{functionName}?api-version=2016-08-01

方法: 删除

代码片段:

 HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Delete, string.Format("https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Web/sites/{name}/functions/{functionName}?api-version=2016-08-01", "Pass All Param In {}")));

request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", results.access_token);

HttpResponseMessage response = await _client.SendAsync(request);
if (response.IsSuccessStatusCode)
{
dynamic objApiResponse = JsonConvert.DeserializeObject<dynamic>(await response.Content.ReadAsStringAsync());

}
else
{
return req.CreateResponse(HttpStatusCode.OK, "Sorry Invalid Request");
}

详情请查看official docs

注意:对于 token 请求,您的资源/范围应为https://management.azure.com。发送请求时传递您的 token 。

更新:

您可以使用 client_credentials 身份验证流程请求 token 。尝试以下格式:

应用程序 ID 和租户 ID 的 Azure 门户凭据:

enter image description here

来自门户的应用程序 secret :

enter image description here

token 端点或 URL:

https://login.microsoftonline.com/YourTenantName.onmicrosoft.com/oauth2/token

请求参数:

grant_type:client_credentials
client_id:b603c7be_Your_App_ID_e6921e61f925
client_secret:Vxf1Sl_Your_App_Secret_2XDSeZ8wL/Yp8ns4sc=
resource:https://graph.microsoft.com

PostMan 示例:

enter image description here

token 响应:

enter image description here

token 的代码片段:

            //Token Request End Point
string tokenUrl = $"https://login.microsoftonline.com/YourTenant/oauth2/token";
var tokenRequest = new HttpRequestMessage(HttpMethod.Post, tokenUrl);

//I am Using client_credentials as It is mostly recomended
tokenRequest.Content = new FormUrlEncodedContent(new Dictionary<string, string>
{
["grant_type"] = "client_credentials",
["client_id"] = "20e08e95-_Your_App_ID_e9c711b0d19e",
["client_secret"] = "+trl[ZFl7l_Your_App_Secret__ghon9",
["resource"] = "https://management.azure.com/"
});

dynamic json;
AccessTokenClass results = new AccessTokenClass();
HttpClient client = new HttpClient();

var tokenResponse = await client.SendAsync(tokenRequest);

json = await tokenResponse.Content.ReadAsStringAsync();
results = JsonConvert.DeserializeObject<AccessTokenClass>(json);


//New Block For Accessing Data from API
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Delete, string.Format("https://management.azure.com/subscriptions/YOurSubscription/resourceGroups/YourResourceGroup/providers/Microsoft.Web/sites/DeleteTestFuncAppName/functions/DeleteFunctionNameThatYouWantToDelete?api-version=2016-08-01"));
//Passing Token For this Request
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", results.access_token);
HttpResponseMessage response = await client.SendAsync(request);
//Read Server Response
dynamic objServerResponse = JsonConvert.DeserializeObject<dynamic>(await response.Content.ReadAsStringAsync());

我使用过的类:

   public class AccessTokenClass
{
public string token_type { get; set; }
public string expires_in { get; set; }
public string resource { get; set; }
public string scope { get; set; }
public string access_token { get; set; }
public string refresh_token { get; set; }

}

要记住的一点:

如果您遇到此错误

InvalidAuthenticationToken: The received access token is not valid: at least one of the claims 'puid' or 'altsecid' or 'oid' should be present. If you are accessing as application please make sure service principal is properly created in the tenant

您必须为您的应用程序分配角色,如下所示:

enter image description here

关于c# - 有没有办法使用 C# 从我的 Function App 中删除 Azure Function?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57377303/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com