Azure.Authentication.ApplicationTokenProvider不对ServiceClientCredentials设置过期时间。它是用于获取应用程序的身份验证令牌,而不是为ServiceClientCredentials设置过期时间。要为ServiceClientCredentials设置过期时间,可以使用ADAL (Azure Active Directory Authentication Library)或MSAL (Microsoft Authentication Library)。
下面是使用ADAL的示例代码:
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.Rest;
...
string clientId = "yourClientId";
string clientSecret = "yourClientSecret";
string tenantId = "yourTenantId";
string authorityUri = $"https://login.microsoftonline.com/{tenantId}";
// Create the credentials using ADAL
var clientCredential = new ClientCredential(clientId, clientSecret);
var authenticationContext = new AuthenticationContext(authorityUri);
var authenticationResult = await authenticationContext.AcquireTokenAsync("https://management.azure.com/", clientCredential);
// Create the ServiceClientCredentials using the access token from ADAL
var tokenCredentials = new TokenCredentials(authenticationResult.AccessToken);
// Use the tokenCredentials with your ServiceClient
var serviceClient = new MyServiceClient(tokenCredentials);
上述代码使用ADAL获取应用程序的访问令牌,然后使用该访问令牌创建ServiceClientCredentials。这样,ServiceClientCredentials将具有与获取的访问令牌相同的过期时间。
请注意,ADAL已被MSAL取代,因此建议使用MSAL。使用MSAL的示例代码类似,只需相应地调整命名空间和类名即可。