简体   繁体   English

授权 Azure REST API 请求

[英]Authorizing an Azure REST API Request

I am trying to write a local console application which will swap an Azure Web App slot using the Azure REST API .我正在尝试编写一个本地控制台应用程序,它将使用Azure REST API交换 Azure Web App 插槽。 Using the following code I get a 401 (Unauthorized) response:使用以下代码,我得到 401(未经授权)响应:

public async Task Swap(string subscription, string resourceGroup, string site, string slot) 
{
    var client = new HttpClient();

    var url =
        $"https://management.azure.com/subscriptions/{subscription}/resourceGroups/{resourceGroup}/providers/Microsoft.Web/sites/{site}/applySlotConfig?api-version=2016-08-01";

    var data = new {preserveVnet = true, targetSlot = slot};

    var message = new HttpRequestMessage
    {
        RequestUri = new Uri(url),
        Method = HttpMethod.Post,
        Content = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json")
    };

    var response = await client.SendAsync(message);

    Console.WriteLine(response.StatusCode);
} 

I know I need to put in some kind of credentials but what I have found seems to apply to apps using Azure AD for authentication.我知道我需要输入某种凭据,但我发现的内容似乎适用于使用 Azure AD 进行身份验证的应用程序。 This will be a publicly accessible web app with anonymous authentication.这将是一个具有匿名身份验证的可公开访问的 Web 应用程序。

Generally speaking you need to attach a Authorization header to the request with the Auth token.一般来说,您需要使用 Auth 令牌将 Authorization 标头附加到请求中。 There are numerous ways of getting it, see this link or this .有多种获取方式,请参阅此链接

This is how I managed to do it (using the provided links):这就是我设法做到的(使用提供的链接):

private async Task<string> GetAccessToken(string tenantName, string clientId, string clientSecret)
{
    var authString = "https://login.microsoftonline.com/" + tenantName;
    var resourceUrl = "https://management.azure.com/";

    var authenticationContext = new AuthenticationContext(authString, false);
    var clientCred = new ClientCredential(clientId, clientSecret);
    var authenticationResult = await authenticationContext.AcquireTokenAsync(resourceUrl, clientCred);
    var token = authenticationResult.AccessToken;

    return token;
}

And then in my previous method:然后在我以前的方法中:

public async Task Swap(string subscription, string resourceGroup, string site, string slot) 
{
    var client = new HttpClient();

    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", await GetAccessToken("XXX", "XXX", "XXX"));

    var url =
            $"https://management.azure.com/subscriptions/{subscription}/resourceGroups/{resourceGroup}/providers/Microsoft.Web/sites/{site}/applySlotConfig?api-version=2016-08-01";

    var data = new {preserveVnet = true, targetSlot = slot};

    var message = new HttpRequestMessage
    {
        RequestUri = new Uri(url),
        Method = HttpMethod.Post,
        Content = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json")
    };

    var response = await client.SendAsync(message);

    Console.WriteLine(response.StatusCode);
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM