7 Replies - 485 Views - Last Post: 15 March 2019 - 06:42 AM

#1 fearfulsc2   User is offline

  • D.I.C Regular

Reputation: 16
  • View blog
  • Posts: 281
  • Joined: 25-May 16

HttpContext not working as Expected

Posted 13 March 2019 - 06:41 AM

Hi everyone, I'm trying to use HttpContext for my Rest APIs so I created a static class to inject HttpContext

public static class HttpContextAccessorMiddleWare
    {
        public static IServiceCollection AddHttpContextAccessor(this IServiceCollection services)
        {
            if(services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();

            return services;
        }
    }



I then added it in the Startup.cs class as
 // middleware
            services.AddHttpContextAccessor();



I then did dependency injection on a health controller
 private readonly IHealthSvc _health;
        private readonly IHttpContextAccessor _httpContextAccessor;

        public HealthController(IHealthSvc health, IHttpContextAccessor httpContextAccessor)
        {
            _health = health;
            _httpContextAccessor = httpContextAccessor;
        }



When I try to do a poke, I wanted to see what the value was, and I keep getting null
 public IActionResult Poke()
        {
            var y = _httpContextAccessor.HttpContext.User.Identity.Name; // I keep getting null here. I don't know why



I keep getting a null value and I do not know why.

Has anyone faced something like this before?

Is This A Good Question/Topic? 0
  • +

Replies To: HttpContext not working as Expected

#2 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6823
  • View blog
  • Posts: 28,274
  • Joined: 12-December 12

Re: HttpContext not working as Expected

Posted 13 March 2019 - 06:45 AM

Is this with ASP.NET Core?
Was This Post Helpful? 0
  • +
  • -

#3 fearfulsc2   User is offline

  • D.I.C Regular

Reputation: 16
  • View blog
  • Posts: 281
  • Joined: 25-May 16

Re: HttpContext not working as Expected

Posted 13 March 2019 - 06:46 AM

yes, this is with ASP.NET Core
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6823
  • View blog
  • Posts: 28,274
  • Joined: 12-December 12

Re: HttpContext not working as Expected

Posted 13 March 2019 - 06:54 AM

Looking at this page it appears the context is already available via ControllerBase as HttpContext - see the section "Use HttpContext from a controller".

Or, further down, it can be added in ConfigureServices services.AddHttpContextAccessor();.

Otherwise, if you still wish to pursue a static class, then I suppose you could try AddTransient as a quick test. (I don't know the technicalities as to how, or whether, this might work as a static class, but I would personally just bung in AddTransient and see if it floats.)

Also,

var y = _httpContextAccessor.HttpContext.User.Identity.Name; // I keep getting null here. I don't know why


you didn't mention which part of this is null.
Was This Post Helpful? 0
  • +
  • -

#5 fearfulsc2   User is offline

  • D.I.C Regular

Reputation: 16
  • View blog
  • Posts: 281
  • Joined: 25-May 16

Re: HttpContext not working as Expected

Posted 13 March 2019 - 07:03 AM

The User.Identity.Name is null but in reality, there are many null values that are in HttpContext.User

But the Identity property in it has something in there that is not of use.
Was This Post Helpful? 0
  • +
  • -

#6 fearfulsc2   User is offline

  • D.I.C Regular

Reputation: 16
  • View blog
  • Posts: 281
  • Joined: 25-May 16

Re: HttpContext not working as Expected

Posted 13 March 2019 - 10:18 AM

I found the issue. In the project settings, I only had Anonymous Authentication turned on.....*bash head on keyboard*
Was This Post Helpful? 1
  • +
  • -

#7 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15357
  • View blog
  • Posts: 61,571
  • Joined: 12-June 08

Re: HttpContext not working as Expected

Posted 13 March 2019 - 10:51 AM

Oh hells bells.. I now remember that setting being a PITA a few months back. Oh joy.
Was This Post Helpful? 0
  • +
  • -

#8 fearfulsc2   User is offline

  • D.I.C Regular

Reputation: 16
  • View blog
  • Posts: 281
  • Joined: 25-May 16

Re: HttpContext not working as Expected

Posted 15 March 2019 - 06:42 AM

Now I am facing another issue...

I have micro services. I have authentication on both of them. I have authorization enabled so only windows-authenticated users can access these services.

We have a service that is supposed to call the other service.

When I run it all locally from my visual studio debugger, everything runs fine and it gets my credentials. If I use the first service locally to call the other service on the server, it works.

When I use the first service from the server to call the second service on the server, the second service rejects it and gives me a 401, unauthorized. I turned off the Authorization for the second service and I am still getting the 401 unauthorized error. I confirmed that it's from IIS since I have Windows Authentication enabled. When I turn off windows authentication from IIS and turn on Anonymous Authentication, the service allows me to call it.

If I log on to the server and use the service from the server in its local domain, it doesn't even like my own credentials and rejects me if windows authentication is turned on. Has anyone faced this before?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1