Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法从.net core中的HttpContext获取User.Name。DI不能解决它_.net_Asp.net Core_Asp.net Identity_Httpcontext - Fatal编程技术网

无法从.net core中的HttpContext获取User.Name。DI不能解决它

无法从.net core中的HttpContext获取User.Name。DI不能解决它,.net,asp.net-core,asp.net-identity,httpcontext,.net,Asp.net Core,Asp.net Identity,Httpcontext,我需要在我的上下文中获取当前应用程序用户信息(继承自IdentityDbContext。为此,我创建了一个UserResolverService,如所述。我在启动时注册了IHttpContextAccessor和UserResolverServices。但即使在服务中,用户仍然保持空值,即使用户已正式登录。我缺少什么 我创建了一个简单的环境,如下所示: 应用程序上下文: public class ApplicationDbContext : IdentityDbContext<Applic

我需要在我的上下文中获取当前应用程序用户信息(继承自IdentityDbContext。为此,我创建了一个UserResolverService,如所述。我在启动时注册了
IHttpContextAccessor
UserResolverServices
。但即使在服务中,用户仍然保持空值,即使用户已正式登录。我缺少什么

我创建了一个简单的环境,如下所示:

应用程序上下文:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    private string _userName;

    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options, UserResolverService userService)
        : base(options)
    {
        _userName = userService.GetActualUser();
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
    }
}
我的配置服务:

public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

        services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

        // Add application services.

        services.AddTransient<IEmailSender, EmailSender>();
        services.AddTransient<IHttpContextAccessor, HttpContextAccessor>();
        services.AddTransient<UserResolverService>();

        services.AddMvc();

    }
public void配置服务(IServiceCollection服务)
{
services.AddDbContext(选项=>
options.UseSqlServer(Configuration.GetConnectionString(“DefaultConnection”));
服务.额外性()
.AddEntityFrameworkStores()
.AddDefaultTokenProviders();
//添加应用程序服务。
services.AddTransient();
services.AddTransient();
services.AddTransient();
services.AddMvc();
}

您忽略了显示最重要的部分:当用户为空时,您在何处以及如何利用您的上下文。@ChrisPratt我不理解您的评论。我正在部署一个具有标识的通用VS2017模板(个人帐户).LoggedIn时,我想从用户处检索TenantId以进行进一步处理。我有一个解决方案,但这需要从当前用户处获取名称。所有说明都告诉我创建服务并为此使用HttpContextAccessor。仅供参考,我使用的是.net core 2.0。
public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

        services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

        // Add application services.

        services.AddTransient<IEmailSender, EmailSender>();
        services.AddTransient<IHttpContextAccessor, HttpContextAccessor>();
        services.AddTransient<UserResolverService>();

        services.AddMvc();

    }