1 回答

TA貢獻2011條經驗 獲得超2個贊
您正在使用應用程序啟動時HttpClient從緩存中檢索的靜態值進行注冊。此時,您的托管服務尚未運行,因此緩存中還沒有任何價值。一旦緩存中最終有一個值,標頭早已被設置,并且您永遠不會重置它。
這里實際上完全不需要緩存。您也不需要Authorization在實際的客戶端注冊中設置標頭。相反,只需將您的GetAccessToken方法修改為:
private void GetAccessToken(object state)
{
Dictionary<string, string> authenticationCredentials_np = Configuration.GetSection("NonProductionEnvironment:Credentials").GetChildren().Select(x => new KeyValuePair<string, string>(x.Key, x.Value)).ToDictionary(x => x.Key, x => x.Value);
Token token_np = GetToken(new Uri(Configuration["NonProductionEnvironment:URL"]), authenticationCredentials_np).Result;
_client_NP.DefaultRequestHeaders.Add("Authorization", $"Bearer {token_np.AccessToken}");
}
- 1 回答
- 0 關注
- 111 瀏覽
添加回答
舉報