using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Text; using System.Threading.Tasks; namespace AttachmentDemo.Helper { public class AuthHttpClient { //private static Lazy InstanceLazy = new Lazy(() => new HttpClient()); public static HttpClient Instance { get; set; } //=> new HttpClient();// InstanceLazy.Value; private AuthHttpClient() { } public static void SetHttpClient(string baseUrl, string token) { var baseAddress = new Uri(baseUrl); // Set your cookies up in the cookie container of an HttpClientHandler var handler = new HttpClientHandler(); handler.CookieContainer.Add(baseAddress, new Cookie("token", token)); // Use that to create a new HttpClient with the same base address Instance = new HttpClient(handler) { BaseAddress = baseAddress }; Instance.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); } } }