代码之家  ›  专栏  ›  技术社区  ›  user2806570

调试ASP.Net Core 2.1时自动登录

  •  2
  • user2806570  · 技术社区  · 7 年前

    我正在尝试在我构建的ASP.net core 2.1应用程序上自动登录以进行调试。

    获取错误:

    HttpContext不能为null。

    Startup.cs 文件

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider ServiceProvider)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }
    
            app.UseHttpsRedirection();
            app.UseStaticFiles();
    
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
    
            app.UseCookiePolicy();
    
            CreateRoles(ServiceProvider).Wait();
    
            if (env.IsDevelopment())
            {
                DeveloperLogin(ServiceProvider).Wait();
            }
        }
    
    
        private async Task DeveloperLogin(IServiceProvider serviceProvider){
    
            var UserManager = serviceProvider.GetRequiredService<UserManager<User>>();
            var signInManager = serviceProvider.GetRequiredService<SignInManager<User>>();
    
            var _user = await UserManager.FindByNameAsync("test@gmail.com");
    
            await signInManager.SignInAsync(_user, isPersistent: false);
    
        }
    

    这是我不久前提出的关于Mac上Windows身份验证的另一个问题的扩展。由于应用程序的性质,我添加了角色管理的核心标识,即使应用程序仍然只使用Windows Auth。

    堆栈跟踪:

        System.AggregateException: "One or more errors occurred. (HttpContext must not be null.)" 
    ---> System.Exception {System.InvalidOperationException}: "HttpContext must not be null."
        at Microsoft.AspNetCore.Identity.SignInManager`1.get_Context()
        at Microsoft.AspNetCore.Identity.SignInManager`1.SignInAsync(TUser user, AuthenticationProperties authenticationProperties, String authenticationMethod)
        at myApp.Startup.DeveloperLogin(IServiceProvider serviceProvider) in /Users/user/Documents/Repositories/myApp/myApp/Startup.cs:135
    
    1 回复  |  直到 7 年前
        1
  •  9
  •   Edward    7 年前

    对于 HttpContext ,它仅在http请求管道期间存在。没有 请求上下文 在里面 Configure

    供使用 Identity ,您需要使用 app.UseAuthentication(); .

    使用sigin with遵循以下步骤 .

    • 配置请求管道。

          app.UseAuthentication();
          if (env.IsDevelopment())
          {
              app.Use(async (context, next) =>
              {
                  var user = context.User.Identity.Name;
                  DeveloperLogin(context).Wait();
                  await next.Invoke();
              });
          }
      
          app.UseMvc(routes =>
          {
              routes.MapRoute(
                  name: "default",
                  template: "{controller=Home}/{action=Index}/{id?}");
          });
      

      你需要打电话 app.UseAuthentication();

    • DeveloperLogin

          private async Task DeveloperLogin(HttpContext httpContext)
      {
      
          var UserManager = httpContext.RequestServices.GetRequiredService<UserManager<IdentityUser>>();
          var signInManager = httpContext.RequestServices.GetRequiredService<SignInManager<IdentityUser>>();
      
          var _user = await UserManager.FindByNameAsync("Tom");
      
          await signInManager.SignInAsync(_user, isPersistent: false);
      
      }