public async Task <string> GetAccessToken() { // 从缓存中获取token TokenCache token = null; try { token = await _cacheManager.GetToken(); } catch { try { await _cacheManager.RemoveToken(); } catch { } } if (token == null || token.IsExpired()) { bool isProcessWithThis = await _cacheManager.Enter(); if (!isProcessWithThis) { _logger.LogInformation($"由其他管理器处理, {Client.ClientID}"); token = await _cacheManager.GetToken(); return(token?.access_token); } // Stopwatch sw = new Stopwatch(); _logRequestTokenStart(_logger, Client.AuthUrl, Client.ClientID, Client.ClientName ?? "", null); try { // 获取Token DateTime dt = DateTime.Now; var tokenResponse = await _processor.GetToken(Client, _clientFactory); if (tokenResponse != null) { token = new TokenCache() { access_token = tokenResponse.access_token, token_type = tokenResponse.token_type, expires_in = tokenResponse.expires_in, LastGetTime = dt }; await _cacheManager.SetToken(token); } } catch (Exception e) { _logRequestTokenError(_logger, Client.AuthUrl, Client.ClientID, Client.ClientName, e); } finally { await _cacheManager.Exit(); } sw.Stop(); _logRequestTokenEnd(_logger, sw.ElapsedMilliseconds, Client.AuthUrl, Client.ClientID, Client.ClientName ?? "", token?.access_token, null); } return(token?.access_token); }