嗨。我创建新的AspNet核心MVC项目,并附加一个自定义的用户管理器,用户存储,与Nhibernate一起工作的登录管理器, 当我尝试身份验证应用程序抛出异常如何解决它时?谷歌中的许多文章对我没有帮助
InvalidOperationException: No IAuthenticationSignInHandler 配置为处理方案的登录:Identity.Application Microsoft.AspNetCore.Authentication.AuthenticationService+d__13.MoveNext() System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
var result = await this.SignInManager.PasswordSignInAsync("960224350816", "ASD123qwe", false,false);
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme,options =>
{
options.ExpireTimeSpan = TimeSpan.FromDays(7);
}
);
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddIdentityCore<User>(options => {
options.Password.RequiredLength = 10;
}).AddSignInManager<ApplicationSignInManger<User>>().AddUserManager<ApplicationUserManager<User>>().AddUserStore<ApplicationUserStore>().AddDefaultTokenProviders();
services.AddScoped<SignInManager<User>, ApplicationSignInManger<User>>();
services.Configure<IdentityOptions>(options =>
{
// Password settings
options.Password.RequireDigit = true;
options.Password.RequiredLength = 8;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = true;
options.Password.RequireLowercase = false;
options.Password.RequiredUniqueChars = 6;
// Lockout settings
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(30);
options.Lockout.MaxFailedAccessAttempts = 10;
options.Lockout.AllowedForNewUsers = true;
// User settings
options.User.RequireUniqueEmail = true;
});
services.ConfigureApplicationCookie(options =>
{
// Cookie settings
options.Cookie.HttpOnly = true;
options.Cookie.Expiration = TimeSpan.FromDays(150);
options.LoginPath = "/Account/Login"; // If the LoginPath is not set here, ASP.NET Core will default to /Account/Login
options.LogoutPath = "/Account/Logout"; // If the LogoutPath is not set here, ASP.NET Core will default to /Account/Logout
options.AccessDeniedPath = "/Account/AccessDenied"; // If the AccessDeniedPath is not set here, ASP.NET Core will default to /Account/AccessDenied
options.SlidingExpiration = true;
});
services.AddMvc();
}
并配置
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
控制器中的操作:
public async Task<IActionResult> Index() {
try {
var result = await this.SignInManager.PasswordSignInAsync("960224350816", "ASD123qwe", false, false);
}
catch (Exception ex) {
var x = 0;
}
return View();
}
最近遇到了同样的问题。 为 IdentityConstants.ExternalScheme 添加 Cookie 解决了这个问题
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = IdentityConstants.ApplicationScheme;
options.DefaultChallengeScheme = IdentityConstants.ApplicationScheme;
options.DefaultSignInScheme = IdentityConstants.ExternalScheme;
}).AddCookie(IdentityConstants.ApplicationScheme, options =>
{
options.Cookie.HttpOnly = true;
options.ExpireTimeSpan = TimeSpan.FromHours(1);
options.LoginPath = "/Account/Signin";
options.LogoutPath = "/Account/Signout";
}).AddCookie(IdentityConstants.ExternalScheme, o =>
{
o.Cookie.Name = IdentityConstants.ExternalScheme;
o.ExpireTimeSpan = TimeSpan.FromMinutes(5.0);
});