2017-06-14 19 views
6

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.

+1

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 –

Trả lời

5

Dường như có một vài thay đổi phá vỡ với Auth 2,0 (https://github.com/aspnet/Announcements/issues/232)

Quá trình cài đặt đã đúng tuy nhiên tôi cần phải làm hai việc:

  1. Sử dụng HttpContext.SignInAsync() (using Microsoft.AspNetCore.Authentication) thay vì HttpContext.Authentication.SignInAsync()
  2. Sử dụng "AuthenticationTypes.Federation" làm loại xác thực (Lưu ý: các giá trị khác dường như không hoạt động và trống sẽ dẫn đến tên người dùng được đặt và IsAuthenticated thành false) var identity = new ClaimsIdentity(claims, "AuthenticationTypes.Federation");

Dưới đây là mã sửa

Trong Startup.cs

// 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.UseAuthentication(); 

     app.UseMvc(routes => 
     { 
      routes.MapRoute(
       name: "default", 
       template: "{controller=Home}/{action=Index}/{id?}"); 
     }); 
    } 

Trong điều khiển

using Microsoft.AspNetCore.Authentication; 
    //... 
    public async Task<IActionResult> Login() 
    { 
     var claims = new List<Claim> 
     { 
      new Claim(ClaimTypes.Name, "joe nobody") 
     }; 
     var identity = new ClaimsIdentity(claims, "AuthenticationTypes.Federation"); 
     var principal = new ClaimsPrincipal(identity); 
     await HttpContext.SignInAsync("MyCookieMiddlewareInstance", principal); 

     return View(); 
    } 
+0

Thay vì khó -coding 'AuthenticationTypes.Federation' bạn có lẽ nên tham khảo' TokenValidationParameters.DefaultAuthenticationType', được mã hóa cứng với giá trị đó trong ['Microsoft.IdentityModel.Tokens'] (https://github.com/AzureAD/azure-activedirectory- identitymodel-extensions-for-dotnet/blob/master/src/Microsoft.IdentityModel.Tokens/TokenValidationParameters.cs) ... trong đó, vì lý do nào đó, bị ẩn trong dự án phần mở rộng AzureAD. Rõ ràng là bùn ... – McGuireV10

Các vấn đề liên quan