"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Guide to Setting Authorization Header with WinRT HttpClient

Guide to Setting Authorization Header with WinRT HttpClient

Posted on 2025-04-18
Browse:537

How to Set Authorization Headers in WinRT's HttpClient?

Adding Authorization Headers to WinRT's HttpClient

WinRT's HttpClient lacks the .NET Credential class for setting authorization headers. This article details a workaround for adding authorization headers, specifically using OAuth tokens.

The Challenge: How to add an authorization header (e.g., for OAuth) to a WinRT HttpClient request?

The Solution: Utilize the AuthenticationHeaderValue class:

httpClient.DefaultRequestHeaders.Authorization = 
    new AuthenticationHeaderValue("Bearer", "Your OAuth token");

This code snippet constructs an authentication header using the "Bearer" scheme and your OAuth token. Adding this to DefaultRequestHeaders.Authorization ensures the header is automatically included in all subsequent requests made by this HttpClient instance.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3