.net core UserManager.GetUserName(用户)在成功登录后返回NULL

.net core UserManager.GetUserName(用户)在成功登录后返回NULL,.net-core,.net Core,我的代码之前是有效的,但经过优化后,代码不起作用,从昨天开始,我一直在试图找出问题所在。。。 问题是,成功登录后,我无法在我的_布局页面中获取用户名。UserManager始终返回null **在我的创业中** public void ConfigureServices(IServiceCollection services) { services.AddDefaultIdentity<AppUser>(config => { config.Sig

我的代码之前是有效的,但经过优化后,代码不起作用,从昨天开始,我一直在试图找出问题所在。。。 问题是,成功登录后,我无法在我的_布局页面中获取用户名。UserManager始终返回null

**在我的创业中**

    public void ConfigureServices(IServiceCollection services)
    {
       services.AddDefaultIdentity<AppUser>(config => { config.SignIn.RequireConfirmedEmail = true; }).AddEntityFrameworkStores<AppIdentityDbContext>();
       ...........
       services.AddMvc();
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseSession();
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            app.UseHsts();
        }

        app.UseStaticFiles();
        app.UseHttpsRedirection();
        app.UseMvcWithDefaultRoute();
        app.UseAuthentication();
        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
        app.Run(async (context) =>
        {
            await context.Response.WriteAsync("Hello World!");
        });
    }
public void配置服务(IServiceCollection服务)
{
services.AddDefaultIdentity(config=>{config.SignIn.RequireConfirmedEmail=true;});
...........
services.AddMvc();
}
公共无效配置(IApplicationBuilder应用程序,IHostingEnvironment环境)
{
app.UseSession();
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
其他的
{
app.UseExceptionHandler(“/Home/Error”);
app.UseHsts();
}
app.UseStaticFiles();
app.UseHttpsRedirection();
app.UseMvcWithDefaultRoute();
app.UseAuthentication();
app.UseMvc(路由=>
{
routes.MapRoute(
名称:“默认”,
模板:“{controller=Home}/{action=Index}/{id?}”);
});
app.Run(异步(上下文)=>
{
wait context.Response.WriteAsync(“Hello World!”);
});
}
AccountController

 public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)    
 {
       await signInManager.SignOutAsync();
       AppUser user = await userManager.FindByEmailAsync(model.Email);

        if (user != null)
        {
            await signInManager.SignOutAsync();
            var result = await signInManager.PasswordSignInAsync(user, model.Password, false, false);

             if (result.Succeeded){ return Redirect("/Home");}
  }
公共异步任务登录(LoginViewModel模型,字符串返回URL)
{
等待signInManager.SignOutAsync();
AppUser=await userManager.findbyemailsync(model.Email);
如果(用户!=null)
{
等待signInManager.SignOutAsync();
var result=await-signInManager.PasswordSignInAsync(user,model.Password,false,false);
if(result.successed){return Redirect(“/Home”);}
}
在Account controller中,用户成功登录,我调用Home index页面。在my_布局中,我希望在导航栏中显示用户名

\u Layout.cshtml

 @using Microsoft.AspNetCore.Identity
 @inject SignInManager<AppUser> SignInManager
 @inject UserManager<AppUser> UserManager

  @if (UserManager.GetUserName(User) == null)
  {
      <a class="btn btn-warning"
         asp-action="Login" asp-controller="Account">Log In</a>
  }
  else
  {
       <a class="btn  btn-danger"
          asp-action="Logout" asp-controller="Account">Log Out(@UserManager.GetUserName(User))</a>
  }
@使用Microsoft.AspNetCore.Identity
@注入SignInManager SignInManager
@注入用户管理器用户管理器
@if(UserManager.GetUserName(User)==null)
{
登录
}
其他的
{
注销(@UserManager.GetUserName(用户))
}

我是.net core新手,我不知道在startup课程中应用程序的顺序很重要。 当我将app.UseAuthentication()移动到Configure方法的顶部时,一切都开始工作。这对我来说没有意义,但它解决了我的问题