我試圖在不使用身份的情況下進行身份驗證。我發現了一些文章,描述瞭如何在其他版本中使用它,但是對於ASP.NET Core 2沒有任何幫助。無身份的Cookie中間件ASP.NET Core v2
下面是我拼湊在一起的內容。但是,當它到達SignInAsync拋出一個異常InvalidOperationException: No authentication handler is configured to handle the scheme: MyCookieMiddlewareInstance
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddCookieAuthentication("MyCookieMiddlewareInstance", o =>
{
o.LoginPath = new PathString("/Account/Login/");
o.AccessDeniedPath = new PathString("/Account/Forbidden/");
});
services.AddAuthentication();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
app.UseAuthentication();
}
public async Task<IActionResult> Login()
{
var claims = new List<Claim>
{
new Claim(ClaimTypes.Name, "joe nobody")
};
var identity = new ClaimsIdentity(claims, "MyCookieMiddlewareInstance");
var principal = new ClaimsPrincipal(identity);
//blows up on the following statement:
//InvalidOperationException: No authentication handler is configured to handle the scheme: MyCookieMiddlewareInstance
await HttpContext.Authentication.SignInAsync("MyCookieMiddlewareInstance", principal);
return View();
}
沒有爲asp.net核心1.x版(https://docs.microsoft.com/en-us/aspnet/core/security/authentication/cookie)微軟文檔但是IApplicationBuilder.UseCookieAuthentication()在V2的貶值,並沒有發現任何解決方案
我沒有設置任何嘗試2.0有關,但我對你的問題非常感興趣,因爲它會影響我。如果你看看他們的一些單元測試,你至少可以知道你應該調用什麼對象:https://github.com/aspnet/Identity/pull/1119/commits/2bdfdb3220b3aca7513455652617af5685d5293c –