我创建了 .net 控制台应用程序,它是我的 web api 的客户端。这两个应用程序都在 azure 中注册。我希望我的控制台应用程序无需用户交互即可运行。控制台应用程序检查消息队列,如果消息到达,它会进行一些计算并将数据发送回我的 Web api。我使用 adal 来验证我的连接。我通过密钥进行身份验证。由于我的客户使用 AutoRest 生成的代码,我添加了 DelegatingHandler 来捕获每个请求并在发送之前添加授权标头:
public class ClientHandler : DelegatingHandler
{
protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
AuthenticationContext authContext = Constants.authContext;
ClientCredential clientCredential = Constants.clientCredential;
string apiId = Constants.apiId;
string tokenType = Constants.tokenType;
// ADAL includes token in memory cache, so this call will only send a message to the server if the cached token is expired.
var result = await authContext.AcquireTokenAsync(apiId, clientCredential);
request.Headers.Authorization = new AuthenticationHeaderValue(tokenType, result.AccessToken);
return await base.SendAsync(request, cancellationToken);
}
}
如您所见,我正在使用已定义的授权上下文。感谢上面的代码,我可以在没有用户交互的情况下获得令牌。而这项工作就好了!但是12 小时后,应用程序开始返回Unauthorized
错误。问题是如何预防?我认为该AcquireToken
方法会处理令牌到期。我错过了什么吗?
编辑: 常量类:
public static class Constants
{
public static string aadInstance = ConfigurationManager.AppSettings["aadInstance"];
public static string tenant = ConfigurationManager.AppSettings["aadTenantName"];
// this application id
public static string clientId = ConfigurationManager.AppSettings["clientApi:ClientId"];
// the key which it can be authenticated
public static string appKey = ConfigurationManager.AppSettings["clientApi:AppKey"];
// the id of the api
public static string apiId = ConfigurationManager.AppSettings["apiId"];
public static string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant);
public static string tokenType = ConfigurationManager.AppSettings["TokenType"];
public static AuthenticationContext authContext = null;
public static ClientCredential clientCredential = null;
public static async Task<TokenCredentials> Authenticate()
{
authContext = new AuthenticationContext(authority);
clientCredential = new ClientCredential(clientId, appKey);
var result = await authContext.AcquireTokenAsync(apiId, clientCredential);
return new TokenCredentials(result.AccessToken, tokenType);
}
}