【问题标题】:EF Core: Get Authenticated username in Shadow PropertiesEF Core:在影子属性中获取经过身份验证的用户名
【发布时间】:2018-07-11 06:57:36
【问题描述】:

作为记录谁输入/更新数据的一部分,我将在所有实体中添加 4 个通用字段(创建者、创建日期、修改者、修改日期)。为此,我正在使用多个论坛中建议的影子属性功能,包括https://dotnetcore.gaprogman.com/2017/01/26/entity-framework-core-shadow-properties/

我的问题是如何获取有关经过身份验证的用户的信息?。如果它是一个控制器,我可以访问 ApplicationUserManager 但在这种情况下,阴影属性位于

AppDbContext : IdentityDbContext 类。

这是一个 asp.net core 2 web API 项目。

非常感谢任何建议。谢谢

【问题讨论】:

    标签: c# entity-framework asp.net-web-api .net-core entity-framework-core


    【解决方案1】:

    您可以使用HttpContext.User.Identity.Name 获取当前用户的名称。您可以使用IHttpContextAccessor 访问HttpContext。该接口应该已经在服务集合中注册。否则,您可以注册它:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
        services.AddDbContext<ApplicationDbContext>(options =>
             options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
        // ...
    }
    

    那么,你就可以从DbContext使用这个接口了:

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
    
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options, IHttpContextAccessor httpContextAccessor)
            : base(options)
        {
            _httpContextAccessor = httpContextAccessor;
        }
    
        public override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default(CancellationToken))
        {
            var httpContext = _httpContextAccessor.HttpContext;
            if(httpContext != null)
            {
                var authenticatedUserName = httpContext.User.Identity.Name;
    
                // If it returns null, even when the user was authenticated, you may try to get the value of a specific claim 
                var authenticatedUserId = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value
                // var authenticatedUserId = _httpContextAccessor.HttpContext.User.FindFirst("sub").Value
    
                // TODO use name to set the shadow property value like in the following post: https://www.meziantou.net/2017/07/03/entity-framework-core-generate-tracking-columns
            }
    
            return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
        }
    }
    

    【讨论】:

    • 感谢您的建议。即使在服务集合中注册 IHttpContextAccessor 后,我也会得到 NULL。任何建议....
    • 哪个属性为空:_httpContextAccessor.HttpContext_httpContextAccessor.HttpContext.User.Identity.Name
    • _httpContextAccessor.HttpContext.User.Identity.Name 为空
    • 只有在用户在 aspnet 管道中通过身份验证后,您才应该调用 SaveChanges。您可以使用_httpContextAccessor.HttpContext.User.Identity.IsAuthenticated 检查用户是否已通过身份验证
    • 是的,我已经这样做了。即使用户通过了身份验证,Name 也是 null.....
    猜你喜欢
    • 2018-06-13
    • 2020-07-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-03
    • 1970-01-01
    • 2011-05-07
    • 2018-11-26
    • 1970-01-01
    相关资源
    最近更新 更多