question

Valdislaf avatar image
0 Votes"
Valdislaf asked Valdislaf published

remote stop, remote start azure-functions

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

JayaC-MSFT avatar image
1 Vote"
JayaC-MSFT answered

@Valdislaf You can leverage the management API endpoints to start and stop the app:
https://docs.microsoft.com/en-us/rest/api/appservice/webapps/start
https://docs.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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Valdislaf avatar image
1 Vote"
Valdislaf answered Valdislaf published

/**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! =)))







5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.