Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Injecting IHttpContextAccessor into ApplicationDbContext ASP.NET Core 1.0

I am trying to figure out how to get access to the current logged in username outside of an ASP.NET Controller.

For example I am trying to do this:

Track Created and Modified fields Automatically with Entity Framework Code First

To setup tracking on entities in the DbContext.

Here is my ApplicationDbContext but I keep getting an error saying _httpContextAccessor is null:

private readonly IHttpContextAccessor _httpContextAccessor;

public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options, IHttpContextAccessor httpContextAccessor)
    : base(options)
{
    _httpContextAccessor = httpContextAccessor;
}
like image 731
Blake Rivell Avatar asked Aug 24 '16 19:08

Blake Rivell


People also ask

Should I use IHttpContextAccessor?

It's only necessary to use IHttpContextAccessor when you need access to the HttpContext inside a service.

Can IHttpContextAccessor be Singleton?

I understand that IHttpContextAccessor is registered as a singleton when services. AddHttpContextAccessor() is called, and that it uses AsyncLocal to access information for the current user. In just about all the examples I've seen, services dependent on IHttpContextAccessor are being registered as scoped or transient.

What is IHttpContextAccessor?

ASP.NET Core applications access the HTTPContext through the IHttpContextAccessor interface. The HttpContextAccessor class implements it. You can use this class when you need to access HttpContext inside a service.

What is ApplicationDbContext in asp net?

The ApplicationDbContext links the database server to the data model classes in the Asp.Net application. This only calls dbContext. Table1. Field1 and has the value of a data table.


2 Answers

Try injecting the IHttpContextAccessor Interface

You can even abstract it further by creating a service to provide just the information you want (Which is the current logged in username)

public interface IUserResolverService {
    string GetUser();
}

public class UserResolverService : IUserResolverService {
    private readonly IHttpContextAccessor accessor;
    public UserResolverService(IHttpContextAccessor accessor) {
        this.accessor = accessor;
    }

    public string GetUser() {
        var username = accessor?.HttpContext?.User?.Identity?.Name ;
        return username ?? "unknown";
    }
}

You need to setup IHttpContextAccessor now in Startup.ConfigureServices in order to be able to inject it:

services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
//OR
//services.AddHttpContextAccessor();
services.AddTransient<IUserResolverService, UserResolverService>();

and pass that to your repository as needed to record associated username

like image 67
Nkosi Avatar answered Oct 15 '22 16:10

Nkosi


@Nkosi has the correct answer but please note that IHttpContextAccessor is now under the namespace:

Microsoft.AspNetCore.Http;

And no longer under:

Microsoft.AspNet.Http;
like image 20
Stephen Brickner Avatar answered Oct 15 '22 14:10

Stephen Brickner