如何在 C# 控制台应用程序中实现 az login
(Azure CLI) 体验?
在这种情况下,将打开一个浏览器窗口,用户进行身份验证,然后他就可以访问私有(private)资源。 我的猜测是身份验证 token 存储在某个地方,但是在哪里呢? session 变量、文件..?
更新
我发现有一个文件夹~/.azure
存储了相关信息。所以问题更多地集中在第一部分(启动浏览器并获取生成的 token )。
最佳答案
How could someone implement the az login (Azure CLI) experience in a C# Console application?
1.使用Process.Start(@"http://url");
启动浏览器。用户输入凭据后,您将获得授权码。复制它。
2.获取授权码。
3.使用以下代码获取访问 token :
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;
}
有关如何获取授权码
和访问 token
的更多详细信息,您可以引用此article .
关于C# 控制台应用程序身份验证 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56015704/