4

I have been trying to implement the http context in the ASP.NET MVC and system.web ,it would just use allow me to use HttpContext.Current to access the context . Anyway I started by injecting the IhttpcontextAccessor in the configureService method in the StartUp Class. I am posting this to see if anyone has implemented this using .Net Core 2.0. If so, please feel free to share the knowledge. Thanks in advance.

 services.AddSingleton<HttpContextAccessor, HttpContextAccessor>();
cedPound
  • 367
  • 1
  • 3
  • 14
  • 1
    HttpContext.Current got removed in .NET core, so you won't be able to use that, see here for more information: https://stackoverflow.com/questions/38571032/how-to-get-httpcontext-current-in-asp-net-core – Sasha Dec 14 '17 at 14:11
  • What are you *actually* trying to do here. What's your real problem? – spender Dec 14 '17 at 14:12
  • @spender i would like to identify the current logged in user – cedPound Dec 14 '17 at 14:14
  • 1
    @cedPound Maybe this will help: https://dotnetcoretutorials.com/2017/01/05/accessing-httpcontext-asp-net-core/ – Maarten Dec 14 '17 at 14:17

2 Answers2

10

If you need it from a Controller, then just use HttpContext, as Muqeet's answer says.

If you do need to inject it, then you're on the right track with AddSingleton, but you have a slight typo:

services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

Notice the I in the first type.

Then set up your constructor:

public class UserService : IUserService {
    private readonly HttpContext _context;

    public UserService(IHttpContextAccessor httpContextAccessor) {
        _context = httpContextAccessor.HttpContext;
    }
}

Then you can use context anywhere else in your class.

That's the basics of dependency injection. AddSingleton makes it available for injection. Then you ask for it where you need it by adding it to your constructor.

That article that Maarten linked to in his comment explains in more detail.

Gabriel Luci
  • 38,328
  • 4
  • 55
  • 84
  • thank you, i think you put me on the right track , i shall mark your answer as the correct one, but if i may ask how can you check the current logged in user inside a method. e.g index method – cedPound Dec 14 '17 at 15:27
  • You use `_context.User`. For example, the name of the account will be in `_context.User.Identity.Name`. – Gabriel Luci Dec 14 '17 at 15:36
  • Thank you, appreciate it – cedPound Dec 14 '17 at 15:44
  • 1
    why get the HttpContext as a dependency when its already available in the ControllerBase as a property? You just do HttpContext.User.Identity.Name right? – Muqeet Khan Dec 14 '17 at 16:04
  • @MuqeetKhan Ha! So it is! Why does everyone talk about dependency injection with it then? That's messed up. Add your own answer. I'll up-vote it. – Gabriel Luci Dec 14 '17 at 16:09
  • @Gabriel-Luci because it's available by inheriting the Controller, which inherits from ControllerBase, and that has HttpContext and User. If you try this in a service, which doesn't inherit Controller, you don't have natural access to it. – Red May 29 '18 at 14:58
7

In Controllers

Looking at your code example it looks like you are trying to get the HttpContext information within a controller. You DO NOT need to inject HttpContextAccessor, as HttpContext is already available for you as a property on the Controller. You can use it as you would any other property.

Example

[Authorize]
public async Task<IActionResult> MySecureAction()
{
   _logger.LogDebug($"Logged in User is {HttpContext.User.Identity.Name}");
   return OK();
}

In Services

If you are trying to get access to the HttpContext object in a service then you would constructor inject the IHttpContextAccessor.

Asp.Net Core 2.1

In the upcoming release for ASP.NET Core 2.1, there will be a helper extension method AddHttpContextAccessor available for you to add it to the service collection properly.

Muqeet Khan
  • 2,094
  • 1
  • 18
  • 28
  • Realizing that I do *not* have to inject HttpContextAccessor just made my life a lot easier. Thanks for mentioning that! – Aaron Jordan Jun 06 '18 at 17:54