Click here to Skip to main content
15,879,239 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to get authorization key values in Rest API Service via c#. Currently I am getting null exception while trying to fetch the key values. I am using application and invoking via postman and the authorization key values are set. I am getting httpcontext as null while debugging.
HttpContext httpContext = HttpContext.Current;

What I have tried:

Code as below:
HttpContext httpContext = HttpContext.Current;

NameValueCollection headerList = httpContext.Request.Headers;
var authorizationField = headerList.Get("Authorization");
Updated 18-Jan-23 19:46pm
Laiju k 19-Jan-23 0:48am    
which .Net version you are using.
Member 7513082 19-Jan-23 0:59am    
I am using 4.6.1 framework. But i am getting null for httpcontext
Graeme_Grant 19-Jan-23 0:51am    
Member 7513082 19-Jan-23 1:00am    
In this i have updated the code more clearly. I am not getting a response for this issue. Request invoked through postman but the httpcontext null issue remains
Richard Deeming 19-Jan-23 6:05am    
If HttpContext.Current is returning null, then you're either executing code on a background thread, or you're executing code outside of the context of an HTTP request.

Since you haven't shown the context of the problematic code, we can't tell you which, nor how to fix it.

1 solution

Watch this to learn how OAuth works: OAuth 2.0 & 2.1 Tutorial - YouTube[^]
Watch this for Authentication: ASP.NET Core Authentication (.NET 7 Minimal Apis C#) - YouTube[^]
Watch this for Authorization: ASP.NET Core Authorization (.NET 7 Minimal Apis C#) - YouTube[^]

He has other videos on the subject.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900