我目前正在構建一個.NET Core應用程式,該應用程式直接執行SharePoint REST呼叫:contoso.sharepoint.com/sites/shipment/_api/search/query?querytext='...'
.NET Core應用程式已在應用程式注冊中注冊。我如何檢索訪問令牌?
uj5u.com熱心網友回復:
你可以使用證書的方式來獲得令牌,像這樣:
private static async Task< string> GetToken()。
{
string applicationId = "xxx"/span>;
string tenantId = "contoso.onmicrosoft.com"。
X509Certificate2 certificate = new X509Certificate2(@"C:certificate.pfx", " password")。
IConfidentialClientApplication confApp = ConfidentialClientApplicationBuilder.Create(applicationId)
.WithAuthority($"https://login.microsoftonline.com/{tenantId}"/span>)
.WithCertificate(certificate) //這只是一個本地方法,在我的機器上獲得證書。
.Build()。
var scopes = new[] { "https://contoso.sharepoint.com/.default"/span> };
var authenticationResult = await confApp.AcquireTokenForClient(scopes).ExecuteAsync()。
return authenticationResult.AccessToken。
uj5u.com熱心網友回復:
我在公共客戶端應用中使用了以下代碼
public async Task< string> GetTokenAsync()
{
var clientId = "{client_id}"/span>。
var tenantId = "{tenant_id}";
var instance = "https://login.microsoftonline.com"/span>;
IPublicClientApplication clientApp = PublicClientApplicationBuilder.Create(clientId)
.WithAuthority($"{instance}/{tenantId}"/span>)
.WithDefaultRedirectUri()
.Build()。
var accounts = await clientApp.GetAccountsAsync()。
var firstAccount = accounts.FirstOrDefault();
var scopes = new[] { "https://contoso.sharepoint.com/.default"/span> };
var userName = "{user}"/span>;
SecureString password = ...;
AuthenticationResult authResult。
try
{
authResult = await clientApp.AcquireTokenSilent(scopes, firstAccount).ExecuteAsync()。
}
catch (MsalUiRequiredException ex)
{
authResult = await clientApp
.AcquireTokenByUsernamePassword(scopes, userName, password)
.ExecuteAsync()。
}
return authResult.AccessToken。
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/308865.html
標籤:
上一篇:<p>我最近開始學習LWJGL,我正試圖讓一個簡單的著色器作業。我遵循了一個"老"教程,但當我運行代碼時,沒有任何形狀出現(視窗打開,但只是黑色)。以下是我的著色器的.
下一篇:將一個資源的變數傳遞給另一個
