Fabian
Fabian

Reputation: 289

Can't get the header value from HTTP header in Asp.Net Core 2.0

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

enter image description here

How to get those user's info (in this case just username) from header so that I can display in my HTML template?

Upvotes: 3

Views: 13504

Answers (1)

Nkosi
Nkosi

Reputation: 247088

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
}

Upvotes: 5

Related Questions