Tôi đang cố gắng xác thực mà không sử dụng danh tính. Tôi đã tìm thấy một vài bài viết mô tả làm thế nào để làm điều đó trong các phiên bản khác tuy nhiên không có gì cho ASP.NET Core 2.Phần mềm trung gian cookie không có bản sắc ASP.NET Core v2
Dưới đây là những gì tôi đã cobbled với nhau. Nhưng khi nó được cho SignInAsync một ngoại lệ được ném 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();
}
Có một tài liệu Microsoft cho phiên bản 1.x asp.net lõi (https://docs.microsoft.com/en-us/aspnet/core/security/authentication/cookie) tuy nhiên IApplicationBuilder.UseCookieAuthentication() được khấu hao trong v2 và đã không tìm thấy bất kì giải pháp nào.
Tôi không phải là thiết lập để cố gắng bất cứ điều gì liên quan đến 2.0, nhưng tôi m rất quan tâm đến câu hỏi của bạn vì nó sẽ ảnh hưởng đến tôi. Nếu bạn nhìn vào một số xét nghiệm đơn vị của họ, bạn có thể có được một ý tưởng về ít nhất những đối tượng bạn nên gọi: https://github.com/aspnet/Identity/pull/1119/commits/2bdfdb3220b3aca7513455652617af5685d5293c –