4

有人如何az login在 C# 控制台应用程序中实现 (Azure CLI) 体验?

在这种情况下,会打开一个浏览器窗口,用户进行身份验证,然后他就可以访问私有资源。我的猜测是身份验证令牌存储在某个地方,但是在哪里?会话变量,文件..?

更新

我发现有一个文件夹~/.azure存储相关信息。所以问题更多在第一部分(启动浏览器并获取生成的令牌)。

4

1 回答 1

0

有人如何在 C# 控制台应用程序中实现 az login (Azure CLI) 体验?

1.启动浏览器Process.Start(@"http://url");。用户输入凭据后,您将获得授权码。复制它。

2.获取授权码。

3.使用以下代码获取访问令牌:

using (var client = new HttpClient())
{
    client.DefaultRequestHeaders.Add("ContentType", "application/json");
    var requestURl = new Uri($"https://login.microsoftonline.com/xxxxxxxxxxxxxxxxxxxx/oauth2/v2.0/token");
    string body = "{\"client_id\": \"3c35ed0b-a441-4c57-9d1c-3a3b0392d9c3\",\"code\":\"the_code_you_copy_in_the_second_step\",\"redirect_uri\": \"https://localhost\",\"grant_type\": \"authorization_code\",\"client_secret\": \"xxxxxxxxxxxxxx\",\"scope\": \"user.read\"}";
    var stringContent = new StringContent(body, Encoding.UTF8, "application/json");
    var response = client.PostAsync(requestURl, stringContent).Result;
}

4.结果:在此处输入图像描述

有关如何获取的更多详细信息authorization codeaccess token您可以参考这篇文章

于 2019-05-07T09:54:43.547 回答