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