fra*_*fra 6 c# azure azure-cli2 az
如何az login在 C# 控制台应用程序中实现 (Azure CLI) 体验?
在这种情况下,将打开浏览器窗口,用户进行身份验证,然后他就可以访问私有资源。我的猜测是身份验证令牌存储在某个地方,但是在哪里呢?会话变量、文件..?
更新
我发现有一个文件夹~/.azure存储了相关信息。所以问题更多地集中在第一部分(启动浏览器并获取结果令牌)。
如何在 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;
}
Run Code Online (Sandbox Code Playgroud)
更多关于如何获取的详细信息authorization code可以access token参考这篇文章。
| 归档时间: |
|
| 查看次数: |
1806 次 |
| 最近记录: |