我正在尝试通过C#从我的Function App中删除Azure函数。 但是在以编程方式删除它时,该功能未在用户界面上显示,但是当我通过高级工具(Kudu)检查它时,仍然可以看到我的Azure函数。
因此,基本上,在删除Azure函数时,我要做的是,将其删除为function.json,这样一来,在Functions App列表中看不到Azure函数(请参见下图)
但是当我去高级Kudu检查它是否已被删除时,我仍然可以看到它,但是没有function.json文件。我之前(大约6个月前)完成了此操作,然后又可以正常工作了。我不知道我做错了什么还是有什么改变。
对代码的任何帮助将不胜感激。
谢谢
编辑:
我所拥有的详细信息是Function App的用户名,密码,URL,名称( https://my-function-app.scm.azurewebsites.net/api/vfs/site/wwwroot )和Azure函数的名称。
六个月前我所做的工作的一些示例代码
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));
答案 0 :(得分:2)
您可以使用REST API来执行此操作。
https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Web/sites/{name}/functions/{functionName}?api-version=2016-08-01
方法: DELETE
代码段:
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
注意:对于令牌请求,您的resource/Scope
应该为https://management.azure.com
。发送请求时传递令牌。
更新:
您可以使用client_credentials
身份验证流程来请求令牌。请尝试以下格式:
应用ID和租户ID的Azure门户凭据:
门户网站的应用程序秘密:
令牌端点或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示例:
响应令牌:
令牌代码段:
//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:收到的访问令牌无效:在 声明“ puid”或“ altsecid”或“ oid”中的至少一项应为 当下。如果您作为应用程序访问,请确保服务 主体在租户中正确创建
您必须将角色分配给您的应用程序,如下所示: