ADAL库生成的令牌仍然可以在AD Graph被弃用后继续使用。但是建议迁移到Microsoft Graph API,并使用最新版本的Microsoft Authentication Library(MSAL)生成令牌。
以下是使用MSAL库生成令牌的示例代码:
string clientId = "";
string authority = "https://login.microsoftonline.com/";
string clientSecret = "";
string[] scopes = new string[] { "https://graph.microsoft.com/.default" };
IPublicClientApplication app = PublicClientApplicationBuilder
.Create(clientId)
.WithAuthority(authority)
.WithRedirectUri("http://localhost") // This can be any URI, as ADAL doesn't require it.
.Build();
var result = app.AcquireTokenForClient(scopes)
.WithClientSecret(clientSecret)
.ExecuteAsync()
.GetAwaiter()
.GetResult();
string accessToken = result.AccessToken;
此代码将生成一个Access Token,该Token可以用于调用Microsoft Graph API中的任何资源。请注意,您需要使用自己的ClientId、Authority和ClientSecret替换示例中的值。