C# 控制台应用程序身份验证 session

标签 c# azure azure-cli2 az

如何在 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;
}

4.结果:enter image description here

有关如何获取授权码访问 token 的更多详细信息,您可以引用此article .

关于C# 控制台应用程序身份验证 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56015704/

相关文章:

azure - 某些 Azure CLI 命令出现 SSL 握手错误

Azure 交互式 shell 历史记录

c# - ToList 和 Find 实现

database - Azure SQL 数据库中的自动调整是否会重新组织和重建索引?

c# - Azure 应用服务应用程序设置值未更新

python - 如何从 Azure Blob 存储并行加载文件?

azure - 将输出保存到在 Azure-CLI DevOps 任务中不起作用的变量

c# - ASP.NET MVC5 - 如何强制 Entity Framework 忽略模型中的属性?

c# - WPF 应用程序 : Controls look like . NET 2.0 中的 WinForms 对话框

c# - 在顶层使用 DependencyInjection,如何将服务传递到架构中?