简体   繁体   English

将IHttpContextAccessor注入ApplicationDbContext ASP.NET Core 1.0

[英]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. 我试图弄清楚如何访问ASP.NET控制器之外的当前登录用户名。

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 . DbContext设置对实体的DbContext

Here is my ApplicationDbContext but I keep getting an error saying _httpContextAccessor is null : 这是我的ApplicationDbContext但我一直收到错误,说_httpContextAccessornull

private readonly IHttpContextAccessor _httpContextAccessor;

public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options, IHttpContextAccessor httpContextAccessor)
    : base(options)
{
    _httpContextAccessor = httpContextAccessor;
}

Try injecting the IHttpContextAccessor Interface 尝试注入IHttpContextAccessor接口

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: 你需要在Startup.ConfigureServices中设置IHttpContextAccessor才能注入它:

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

and pass that to your repository as needed to record associated username 并根据需要将其传递到您的存储库以记录相关的用户名

@Nkosi has the correct answer but please note that IHttpContextAccessor is now under the namespace: @Nkosi有正确的答案,但请注意IHttpContextAccessor现在在命名空间下:

Microsoft.AspNetCore.Http;

And no longer under: 不再是:

Microsoft.AspNet.Http;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在ASP.NET Core 1.0的DI中的Startup class中添加IHttpContextAccessor? - How to add IHttpContextAccessor in the Startup class in the DI in ASP.NET Core 1.0? 在 ASP.NET Core 中处理 ApplicationDbContext - Dispose ApplicationDbContext in ASP.NET Core ASP.NET 核心 (3.1) 与 EF 核心迁移和 IHttpContextAccessor - ASP.NET Core (3.1) with EF Core Migrations and IHttpContextAccessor 如何在IHttpcontextAccessor的ASP.NET Core 2.0中使用依赖项注入 - How to use dependency injection in ASP.NET Core 2.0 for IHttpcontextAccessor 在 ApplicationDbContext 类 (ASP.NET Core) 中访问经过身份验证的用户信息 - Accessing Authenticated User Info in ApplicationDbContext class (ASP.NET Core) ASP.NET Core 2.1从类库引用ApplicationDbContext - ASP.NET Core 2.1 Referring to ApplicationDbContext from Class Library 如何将ApplicationDbContext绑定到ASP.Net Core 2.2中的View - How to bind ApplicationDbContext to a View in ASP.Net Core 2.2 如何在类/单独线程ASP.Net Core中使用ApplicationDbContext - How to use ApplicationDbContext in a Class / Separate Thread ASP.Net Core 在 ASP.NET Core 中间件中注入服务 - Injecting Service in Middleware in ASP.NET Core 在 ASP.NET Core 中的方法中注入依赖 - Injecting Dependency in Method in ASP.NET Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM