旧版 ADAL 库通过 Client App ID 和 Client Secret 获取 token ,例如:
var clientID = "";
var clientSecret = "";
var aadTenantDomain = "tenant domain";
var context = new AuthenticationContext(string.Format("https://login.windows.net/{0}", aadTenantDomain), false);
var clientCred = new Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential(clientID, clientSecret);
var authenticationResult = await context.AcquireTokenAsync("https://graph.microsoft.com", clientCred).ConfigureAwait(false);
return authenticationResult.AccessToken;
给定 AD 应用 ClientId 和客户端 key ,如何使用 MSAL 获取访问 token ?
谢谢!
最佳答案
引用下面的代码从客户端 ID 和客户端 key 获取访问 token 。
代码:-
using Microsoft.Identity.Client;
var clientID = "xxxxxxxx-xxxx-45b5-b838-6d26a31435cb";
var clientSecret = "xxxxxxxxxxxxxxxxxxxHiOEM6fDAQQ7BNiOLPaH.";
var aadTenantDomain = "xxxxxxxsaioutlook.onmicrosoft.com";
var authority = $"https://login.microsoftonline.com/{aadTenantDomain}";
var clientApplication = ConfidentialClientApplicationBuilder.Create(clientID)
.WithClientSecret(clientSecret)
.WithAuthority(authority)
.Build();
var scopes = new string[] { "https://graph.microsoft.com/.default" };
var authenticationResult = await clientApplication.AcquireTokenForClient(scopes)
.ExecuteAsync()
.ConfigureAwait(false);
var accesstoken = authenticationResult.AccessToken;
Console.WriteLine(accesstoken);
输出:-
关于c# - 如何使用 Microsoft 身份验证库 (MSAL) 通过 AD 应用 ClientId 和 Client Secret 获取 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75394006/