簡體   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