繁体   English   中英

如何在 C# 中重现 powershell > az login 的结果?

[英]how reproduce result of powershell > az login in c#?

“az login”的结果是订阅列表:就像这里: https : //docs.microsoft.com/en-us/rest/api/resources/subscriptions/list?source=docs

如何在不提供tenantId或clientId的情况下为这个请求制作令牌,它是如何在网站登录时制作的?

我可以使令牌非常接近所需,但没有我在网站令牌中看到的内容:

var functionCred = new Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential(clientId, secret);
var context = new AuthenticationContext("https://login.microsoftonline.com/" + tenantId, false);
var result = context.AcquireTokenAsync("https://management.core.windows.net/", functionCred).Result;
var token44 = result.AccessToken;

我应该怎么做才能改进令牌?

默认情况下, az login命令使用用户帐户登录 CLI 将尝试启动 Web 浏览器以交互方式登录,如果浏览器不可用,则 CLI 将回退到设备代码登录。

对于这个问题——

如何在不提供tenantId或clientId的情况下为这个请求制作令牌,它是如何在网站登录时制作的?

在不使用tenantId 和clientId 的情况下获取身份验证令牌是不可行的。 令牌主要由客户端 ID 和机密生成。 Token可以通过app注册+tenentId或者用户凭证+tenentId获取。

下面的代码片段显示了如何使用 Azure Active Directory 身份验证库 (ADAL) 获取访问令牌。

static AuthenticationResult AccessToken() 
{ 
    string resourceUri = "https://datacatalog.azure.com";

    // register a client app and get a Client ID
    string clientId = clientIDFromAzureAppRegistration; 

    //A redirect uri gives AAD more details about the specific application that it will authenticate. 
    string redirectUri = "https://login.live.com/oauth20_desktop.srf"; 

    // Create an instance of AuthenticationContext to acquire an Azure access token 
    string authorityUri = "https://login.windows.net/common/oauth2/authorize";

    AuthenticationContext authContext = new AuthenticationContext(authorityUri); 

    // AcquireToken takes a Client Id that Azure AD creates when you register your client app. 
    return authContext.AcquireToken(resourceUri, clientId, new Uri(redirectUri), PromptBehavior.RefreshSession); 

}

有关详细信息,请查看此步骤以获取Microsoft 文档的访问令牌部分。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM