c# - 休息调用 Azure

标签 c# azure rest

我正在尝试使用我的 C# 代码登录 Azure,并且能够在网上找到类似的内容。

不确定要在此处的作用域数组中传递什么内容,一旦获得访问 token ,如何进行其余调用?

public static string getAccessToken(string[] scopes)
{
   var interactiveCredential = new InteractiveBrowserCredential();
   return interactiveCredential.GetToken(new Azure.Core.TokenRequestContext(scopes, null)).Token;
}

最佳答案

首先创建一个 Azure AD 应用程序:

Follow this link

然后从 AD 应用程序获取租户 ID 、客户端 ID 、客户端 key ,并使用此类来查询您的 Azure 订阅资源。

class CustomLoginCredentials : ServiceClientCredentials
{
    //Variables
    private static string tenantId = "<Tenant ID goes here>";
    private static string clientId = "<Client ID goes here>";
    private static string clientSecret = "<Client Secret goes here>";
    private static string windowsURL = "https://login.windows.net/";
    private static string azureManagementURL = "https://management.azure.com/";


    private string AuthenticationToken { get; set; }

    public override void InitializeServiceClient<T>(ServiceClient<T> client)
    {
        var authenticationContext =
            new AuthenticationContext(windowsURL + tenantId);
        var credential = new ClientCredential(clientId, clientSecret);

        var result = authenticationContext.AcquireTokenAsync(azureManagementURL,
            clientCredential: credential).Result;

        AuthenticationToken = result.AccessToken;
    }
    public override async Task ProcessHttpRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", AuthenticationToken);
        await base.ProcessHttpRequestAsync(request, cancellationToken);
    }
}

示例:

private async void GetAzureResourcesConsumption()
        {
            var credentials = new CustomLoginCredentials();
            ConsumptionManagementClient client = new ConsumptionManagementClient(credentials);
            client.SubscriptionId = subscriptionId;

            var resources = await client.UsageDetails.ListAsync(null, null, null, top: NumberOfItems);
            var results = resources.ToList<UsageDetail>();
        } 

您的意思是获取访问 token 吗?

private static string GetAuthorizationToken()
    {
        ClientCredential cc = new ClientCredential(ClientId, ServicePrincipalPassword);
        var context = new AuthenticationContext("https://login.windows.net/" + AzureTenantId);
        var result = context.AcquireTokenAsync("https://management.azure.com/", cc);
        if (result == null)
        {
            throw new InvalidOperationException("Failed to obtain the JWT token");
        }

        return result.Result.AccessToken;
    }

关于c# - 休息调用 Azure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71038356/

相关文章:

c# - Asp .Net Identity - 自定义 IUserStore FindByNameAsync 方法

c# - Nhibernate .Fetch 调用在模拟 session 中失败

azure - 将进程内 COM DLL 与 Azure Function 结合使用

c# - CosmosDb 使用 insertMany 请求率很大

rest - 如何公开 URL 友好的 UUID?

api - 与来自 Clojure 的 REST API 交互

c# - 有没有办法强制异步/等待在第一个可用线程上继续?

c# - 如何记录第 3 方应用程序出现消息框的时间?

azure - 在 PowerShell Runbook 中使用 Azure 文件共享

java - Apache Shiro authc.loginUrl 登录后没有重定向到我的 Jersey 服务