remote stop, remote start azure-functions

Valdislaf 26 Reputation points
2020-11-09T10:46:21.057+00:00

Good afternoon!
There is a working azure-function.
How can I implement remote stop, remote start azure-function in C# (visualstudio).
How to access management (start/stop https://[myname].azurewebsites.net/api/[myfunction]).
As a result, it must be remotely stopped and started in Azure using a program in C#.
Thanks.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,605 questions
0 comments No comments
{count} votes

Accepted answer
  1. JayaC-MSFT 5,526 Reputation points
    2020-11-09T19:51:13.843+00:00

    @Valdislaf You can leverage the management API endpoints to start and stop the app:
    https://learn.microsoft.com/en-us/rest/api/appservice/webapps/start
    https://learn.microsoft.com/en-us/rest/api/appservice/webapps/stop
    You can use Httpclient to make an API call to the endpoints mentioned in the document. You may need to pass an authorization token for validation , for that please refer to this

    Please let me know whether this makes sense. If yes, please don't forget to "accept the answer" and "up-vote" so that it could help others in the community.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Valdislaf 26 Reputation points
    2020-11-11T10:31:28.713+00:00

    /**Azure Active Subscription
    Azure CLI Jump or Cloud Shell Jump
    1.Open elevated CMD, type az login and press Enter.

    >az login
    

    To sign in, use a web browser to open the page https://microsoft.com/devicelogin Jump and enter the code ######### to authenticate.
    2.Open in a web browser the page https://microsoft.com/devicelogin, enter the code ######## to authenticate the device on azure.
    3.After we authenticate the device to connect Azure we can continue with the next commands and the first is used to set the default subscription.

    >az account set  --subscription "########-####-####-####-############"
    

    4.Create Azure Service Principal

    >az ad sp create-for-rbac -n "Azure_Service_Principal_Name"
    

    result:
    "appId": "e4faxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx2",
    "displayName": "Azure_Service_Principal_Name",
    "name": "http://Azure_Service_Principal_Name",
    "password": "8xxxxxxxxxxxxxxxxxxx-xxxxxxxxxxxxx",
    "tenant": "d8xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxc34"
    and made this:
    **/
    *

    using Newtonsoft.Json.Linq;
    using System;
    using System.Net.Http;
    using System.Net.Http.Headers;
    using System.Text;
    
    namespace token_create
    {
        class Program
        {
            static async System.Threading.Tasks.Task Main(string[] args)
            {
                var token = String.Empty;
                var responseString = String.Empty;
                using (HttpClient client = new HttpClient())
                {
                    var tokenEndpoint = @"https://login.microsoftonline.com/d8xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxc34/oauth2/token";
                    var accept = "application/json";
    
                    client.DefaultRequestHeaders.Add("Accept", accept);
                    string postBody = @"resource=https://management.azure.com
      &client_id=e4faxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx2
      &grant_type=client_credentials  
      &client_secret=8xxxxxxxxxxxxxxxxxxx-xxxxxxxxxxxxx
      ";
    
                    using (var response = await client.PostAsync(tokenEndpoint, new StringContent(postBody, Encoding.UTF8, "application/x-www-form-urlencoded")))
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            var jsonresult = JObject.Parse(await response.Content.ReadAsStringAsync());
                            token = (string)jsonresult["access_token"];
                        }
                    }
                    //string stop = $"https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Web/sites/{name}/stop?api-version=2019-08-01";
                    string start = $"https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Web/sites/{name}/start?api-version=2019-08-01";
    
                    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, start);
                    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);               
    
                    HttpResponseMessage response0 = client.SendAsync(request).Result;
                    responseString = response0.Content.ReadAsStringAsync().Result;
                }
                Console.WriteLine("Hello World!");
            }
        }
    }
    

    // now it work! =)))

    1 person found this answer helpful.
    0 comments No comments