调试时自动登录 ASP.Net Core 2.1
Auto Login on debug ASP.Net Core 2.1
我正在尝试在我构建的 ASP.net 核心 2.1 应用程序上自动登录以进行调试。
获取错误:
HttpContext must not be 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);
}
这是我不久前问过的另一个问题的扩展,关于 Windows Mac 上的身份验证。由于应用程序的性质,我为角色管理添加了 Core Identity,即使该应用程序仍然只使用 Windows Auth。
自从我迁移到 Macbook 进行开发后,我尝试在构建时自动登录以使用已经存在的身份进行调试,因为没有 Windows Auth 这就是 DeveloperLogin 函数适合的地方in 但是我得到了上面提到的错误。
堆栈跟踪:
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
对于HttpContext
,它只存在于http请求管道中。 Configure
方法中没有HttpContext
,需要参考中间件中的代码。
要使用Identity
,您需要使用app.UseAuthentication();
。
使用带有 Identity
的 sigin 执行以下步骤。
配置请求管道。
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();
,命令为import。
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);
}
我正在尝试在我构建的 ASP.net 核心 2.1 应用程序上自动登录以进行调试。
获取错误:
HttpContext must not be 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);
}
这是我不久前问过的另一个问题的扩展,关于 Windows Mac 上的身份验证。由于应用程序的性质,我为角色管理添加了 Core Identity,即使该应用程序仍然只使用 Windows Auth。
自从我迁移到 Macbook 进行开发后,我尝试在构建时自动登录以使用已经存在的身份进行调试,因为没有 Windows Auth 这就是 DeveloperLogin 函数适合的地方in 但是我得到了上面提到的错误。
堆栈跟踪:
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
对于HttpContext
,它只存在于http请求管道中。 Configure
方法中没有HttpContext
,需要参考中间件中的代码。
要使用Identity
,您需要使用app.UseAuthentication();
。
使用带有 Identity
的 sigin 执行以下步骤。
配置请求管道。
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();
,命令为import。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); }