Can't get the header value from HTTP header in Asp.Net Core 2.0
Do not return null
from a controller action.
Check that the header actually exists before trying to access it.
Refactor the action to be coded a little more defensively.
[HttpGet]
public async Task<IActionResult> GetUser() {
//was the header provided?
var headerValue = Request.Headers["HTTP_JHED_UID"];
if(headerValue.Any() == false) return BadRequest(); //401
//does the person exist?
var person = headerValue.ToString();
if(repository.GetJhedUser(person) == null) return NotFound(); //404
var user = await repository.GetUser(person);
var userDto = mapper.Map<User, UserForDisplayDto>(user);
return Ok(userDto); //200
}
The repository code for GetJhedUser
has a problem as well because calling to string on the linq expression is not doing what you think it does.
I would go further to suggest refactoring GetJhedUser
to what it is actually checking
public bool JhedUserExists(string value) {
return context.Users.FirstOrDefault(x => x.JHED_ID == value) != null;
}
this results in the action looking like this
[HttpGet]
public async Task<IActionResult> GetUser() {
//was the header provided?
var headerValue = Request.Headers["HTTP_JHED_UID"];
if(headerValue.Any() == false) return BadRequest(); //401
//does the person exist?
var person = headerValue.ToString();
if(repository.JhedUserExists(person) == false) return NotFound(); //404
var user = await repository.GetUser(person);
var userDto = mapper.Map<User, UserForDisplayDto>(user);
return Ok(userDto); //200
}
Related videos on Youtube
Fabian
Updated on June 04, 2022Comments
-
Fabian almost 2 years
I have an MVC controller that aims to read the user's info from HTTP header. The header contains user info that returns from a single sign on (SiteMinder). SiteMinder redirects the user to the company login page and returns back to my app along with cookie and HTTP header. The HTTP header contains user's info. My controller is supposed to get those info and display it in the navigation bar.
Here is the controller code:
[HttpGet] public async Task<IActionResult> GetUser() { var person = Request.Headers["HTTP_JHED_UID"].ToString(); if (repository.GetJhedUser(person) != null) { var user = await repository.GetUser(person); var userDto = mapper.Map<User, UserForDisplayDto>(user); return Ok(userDto); } else { return null; } }
And here is my corresponded repository:
public string GetJhedUser(string value) { return context.Users.Where(x => x.JHED_ID == value).ToString(); } public async Task<User> GetUser(string id, bool includeRelated = true) { if(!includeRelated) return await context.Users.FindAsync(id); return await context.Users .SingleOrDefaultAsync(s => s.JHED_ID == id); }
I receive 500 server error. I am sure that the header has those values.
EDIT: Here is an image of debugging. Even I tried with "Date" the value is nothing. Please note that I refactored my codes to @Nkosi's code below
How to get those user's info (in this case just username) from header so that I can display in my HTML template?
-
MCoder about 6 yearshave you tried to check - if (Request.Headers["HTTP_JHED_UID"].Count() > 0) ?
-
Fabian about 6 yearsYes. I still get 500 server error.
-
VMAtm about 6 yearsIt should be
if (Request.Headers["HTTP_JHED_UID"].Any())
, but the main problem is - what exact exception do you get? Syntax error in JSON response means that you got an error html page in response, so what's in there? -
Fabian about 6 yearsI refactored my code to @Nkosi 's code below. I am now getting
BadRequest
. I don't think the issue is on the client side. Somehow the controller doesn't get the header value. -
Nkosi about 6 yearsAt least now you are in a better position to investigate what the true problem is because the action is providing a relevant response.
-
-
Fabian about 6 yearsNope! Still no luck. I've just added my debug window with
Date
header. -
Nkosi about 6 years@FabianEssenberg What headers does the request actually have? check that
-
Fabian about 6 yearsRequest header has both
Date
andHTTP_JHED-UID
. -
Fabian about 6 yearsSeems like either
Request.Headers["Date"]
orRequest.Headers["HTTP_JHED_ID"]
is retrieved. Basically, I am getting nothing from the header. Not even standard ones.