Accessing Identity And HttpContext Info Using Dependency Injection In .NET Core

If you have been using .NET Core, then you know that Dependency Injection is rather important in this new ecosystem. Thankfully, the development team made it incredibly easy to use.

One common scenario that I have seen arise since its release, has been something to the extent of:

"How can I inject Identity or User information via D.I. in .NET Core?"

This could be a common use case for systems that require identity information for server-level transactions (e.g. when creating a new entity, you might want to know who performed this action or likewise if you wanted to audit who accessed a particular record in your system).

This post will cover implementing such services that you can easily use or extend within your applications; which you have probably tried already, but didn't work.

The most common place that you'll see developers go to access the current user might be as simple as the User.Identity property that is exposed from the HttpContext. However, if you are reading this post, I'm sure that you have attempted something like the following.

  1. // Example of a controller  
  2. public FooController(FooContext context)    
  3. {  
  4.        // Injecting your data context (a common-use case)  
  5.        _context = context;  
  6.        // Attempting to resolve the current user  
  7.        _user = HttpContext.User?.Identity?.Name;  
  8. }  
You'll quickly find that the actual HttpContext doesn't exist within the constructor for the Controller (and isn't created until further down the pipeline).

So, in order for us to actually access anything within the HttpContext, we will need to explicitly inject it. Thankfully, that isn't too difficult to do.

Building a Service to Inject the HttpContext

For D.I. to work, we will need to build a service that can inject the HttpContext that we need in the pipeline so that we can access the properties of it.

We can do this by creating a simple class that already injects that information into it, behind the scenes via a IHttpContextAccessor object, as seen below.
  1. public class UserResolverService    
  2. {  
  3.     private readonly IHttpContextAccessor _context;  
  4.     public UserResolverService(IHttpContextAccessor context)  
  5.     {  
  6.         _context = context;  
  7.     }  
  8.   
  9.     public string GetUser()  
  10.     {  
  11.        return await _context.HttpContext.User?.Identity?.Name;  
  12.     }  
  13. }  
What this will do is inject the HttpContext object from a request into this UserResolverService, which will store the context and expose a method called GetUser() that will return the current name of the user.

This might be used within a repository if you needed to store the username that was accessing a particular record.
  1. public class FooRepository : IFooRepository    
  2. {  
  3.       private FooContext _context;  
  4.       private string _currentUser;  
  5.       public FooRepository(FooContext context, UserResolverService userService)  
  6.       {  
  7.           _context = context;  
  8.           _currentUser = userService.GetUser();  
  9.       }  
  10.   
  11.       public void DoWork()  
  12.       {  
  13.            var widget = new Widget(){  
  14.                 Id = 42,  
  15.                 Title = "The Answer",  
  16.                 Author = "Deepthought",  
  17.                 CreatedBy = _currentUser  
  18.            };  
  19.   
  20.            _context.Widgets.Add(widget);  
  21.            _context.SaveChanges();  
  22.       }  
  23. }  
And, that's basically it.

Extending the Service

If you had some additional properties that existed on the ApplicationUserobject (the abstraction that Entity Framework uses by default), then you might want to extend this service to pass along the entire object itself.

Doing this would require a very minor change, as you would simply need to inject your UserManager object into this new service.
  1. public class UserResolverService    
  2. {  
  3.       private readonly IHttpContextAccessor _context;  
  4.       private readonly UserManager<ApplicationUser> _userManager;  
  5.       public UserResolverService(IHttpContextAccessor context, UserManager<ApplicationUser> userManager)  
  6.       {  
  7.             _context = context;  
  8.             _userManager = userManager;  
  9.       }  
  10.       public async Task<ApplicationUser> GetUser()  
  11.       {  
  12.             return await _userManager.FindByEmailAsync(_context.HttpContext.User?.Identity?.Name);  
  13.       }  
  14. }  
This is obviously just a starting point for building a service that leverages the existing context (enabling all sorts of context-sensitive logic), but hopefully it will pique your interest in developing your own services to pass around.

 

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now