我们在尝试从Service Fabric应用程序读取Azure Key Vault的机密时遇到以下异常。
应用程序使用客户端证书对AAD进行身份验证,并访问KeyVault来获取机密。
此问题是间歇性发生的。
有没有一种方法可以确定根本原因,从而可以防止再次发生同一错误。
Message: AADSTS70002: Error validating credentials. AADSTS50012: Client assertion is not within its valid time range.
Trace ID: 333ee9c1-c74f-432d-824a-000f38a0e400
Correlation ID: 35b5cadf-c538-4f75-b1fb-56c4743088f4
Timestamp: 2018-10-24 06:23:30Z
......
答案 0 :(得分:0)
客户端断言不在其有效时间范围内。
根据您的错误消息,您的问题间歇性地发生,我认为这可能是您的令牌的区域时间引起问题。地区时间可能与令牌的有效时间有一定的时间间隔。
因此,我建议您可以使用DateTime.UtcNow
作为标准设置令牌的开始时间和结束时间。这是您可以参考的代码示例。
private static async Task<string> GetClientAssertiotokenAsync(string tenantId,string clientId)
{
X509Certificate2 cert = new X509Certificate2(@"D:\Joey\Documents\joey.pfx", "password", X509KeyStorageFlags.MachineKeySet);
var now = DateTime.UtcNow;
var tokenHandler = new JwtSecurityTokenHandler();
var tokenDescriptor = new SecurityTokenDescriptor
{
Audience = $"https://login.microsoftonline.com/{tenantId}/oauth2/token",
Issuer = clientId,
NotBefore = now.AddHours(1),
Expires = now.AddHours(3),
Subject = new ClaimsIdentity(new[] {
new Claim("sub",clientId)}),
SigningCredentials = new X509SigningCredentials(cert)
};
SecurityToken token = tokenHandler.CreateToken(tokenDescriptor);
string tokenString = tokenHandler.WriteToken(token);
}
有关更多详细信息,您可以参考此article。