InvalidOperationException:没有配置IAuthenticationSignInHandler来处理以下方案的登录:Identity.Application [英] InvalidOperationException: No IAuthenticationSignInHandler is configured to handle sign in for the scheme: Identity.Application
问题描述
嗨.我创建了一个新的AspNet核心MVC项目,并在其上附加了与Nhibernate一起使用的自定义UserManager,UserStore,SignInManager, 以及当我尝试对应用程序进行身份验证时抛出异常 怎么解决呢? google中的许多文章对我没有帮助
Hi. I create new AspNet core MVC project AND attached to it a custom UserManager,UserStore,SignInManager that work with Nhibernate, and when i am trying to auth application throw exception How to Solve it ? many articles in google dont help me
InvalidOperationException:没有配置IAuthenticationSignInHandler来处理以下方案的登录:Identity.Application Microsoft.AspNetCore.Authentication.AuthenticationService + d__13.MoveNext() System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)
InvalidOperationException: No IAuthenticationSignInHandler is configured to handle sign in for the scheme: 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添加Cookie.ExternalScheme解决了该问题
Recently had the same problem. Adding Cookie for IdentityConstants.ExternalScheme solved it
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);
});
这篇关于InvalidOperationException:没有配置IAuthenticationSignInHandler来处理以下方案的登录:Identity.Application的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!