Adding authorization to the headers

前端 未结 5 1370
悲&欢浪女
悲&欢浪女 2020-12-09 03:03

I have the following code:

...
AuthenticationHeaderValue authHeaders = new AuthenticationHeaderValue(\"OAuth2\", Contract.AccessToken);
string result = await         


        
5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-09 03:20

    Maybe intresting for other people. Since I searched on this for a long time. But you have to save your cookies also and give it with your next request. First this is how i got my authentication code and hold my cookies in a static variable (in the first time i call this method I give an empty value to token).

        public static CookieContainer CookieContainer;
        public static async Task Post( TRequest requestBody, string path, string token = "")
        {
            var baseUrl = new Uri($"urlFromApi");
            CookieContainer = new CookieContainer();
            using (var handler = new HttpClientHandler() { CookieContainer = CookieContainer })
                using(var client = new HttpClient(handler){BaseAddress = baseUrl})
            {
                client.DefaultRequestHeaders.ConnectionClose = false;
                if (!string.IsNullOrWhiteSpace(token))
                {
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", $"{token}");
                }
                ServicePointManager.FindServicePoint(client.BaseAddress).ConnectionLeaseTimeout = 60 * 1000; //1 minute            using (var content = new ByteArrayContent(GetByteData(requestBody)))
                using (var content = new ByteArrayContent(GetByteData(requestBody)))
                {
                    content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                    var response = await client.PostAsync(String.Empty, content);
                    return await GetResponseContent(response);
                }
            }
    
        }
    

    After this if I do any request to the api I include the cookies (token is what you get from the first response as a result) public static async Task Get(string path, string token = "") {

            var baseUrl = $"https://innoviris-ai.collibra.com/rest/2.0{path}";
            using (var handler = new HttpClientHandler() { CookieContainer = CookieContainer })
            using (var client = new HttpClient(handler) {BaseAddress = new Uri(baseUrl)})
            {
                client.DefaultRequestHeaders.ConnectionClose = false;
                if (!string.IsNullOrWhiteSpace(token))
                {
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", $"{token}");
                }
                ServicePointManager.FindServicePoint(client.BaseAddress).ConnectionLeaseTimeout = 60 * 1000; //1 minute     
    
                var response = await client.GetAsync(String.Empty);
                return await GetResponseContent(response);
            }
        }
    

提交回复
热议问题