2017-06-14 13 views
6

Próbuję uwierzytelniać bez użycia tożsamości. Znalazłem kilka artykułów opisujących, jak to zrobić w innych wersjach, jednak nic w ASP.NET Core 2.Cookie Middleware bez Identity ASP.NET Rdzeń v2

Poniżej jest to, co skleciłem razem. Ale kiedy robi się SignInAsync jest wyjątek 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(); 
    } 

Istnieje dokument Microsoft dla rdzenia asp.net v1.x (https://docs.microsoft.com/en-us/aspnet/core/security/authentication/cookie) jednak IApplicationBuilder.UseCookieAuthentication() jest amortyzowana w v2 i nie znalazłem każdy roztwór.

+1

nie jestem ustawiony tak, aby spróbować cokolwiek 2,0 spokrewnione, ale ja” jestem bardzo zainteresowany w swoim pytaniu, ponieważ wpłynie to na mnie. Jeśli spojrzeć na niektóre z ich testów jednostkowych, może być w stanie zorientować się, co najmniej, jakie obiekty powinny zadzwonić: https://github.com/aspnet/Identity/pull/1119/commits/2bdfdb3220b3aca7513455652617af5685d5293c –

Odpowiedz

5

Wygląda na to, istnieje kilka łamanie zmiany z Auth 2,0 (https://github.com/aspnet/Announcements/issues/232)

Konfiguracja była poprawna jednak muszę zrobić dwie rzeczy:

  1. Wpisz HttpContext.SignInAsync() (using Microsoft.AspNetCore.Authentication) zamiast HttpContext.Authentication.SignInAsync()
  2. Użyj "AuthenticationTypes.Federation" jako typu uwierzytelniania (Uwaga: inne wartości nie wydają się działać, a puste spowoduje ustawienie nazwy użytkownika i IsAuthenticated na false) var identity = new ClaimsIdentity(claims, "AuthenticationTypes.Federation");

Poniżej znajduje się poprawiony kod

W 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?}"); 
     }); 
    } 

kontroler

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

Zamiast ciężko -coding 'AuthenticationTypes.Federation' powinieneś odwoływać' TokenValidationParameters.DefaultAuthenticationType', który jest zakodowany do tej wartości w [ 'Microsoft.IdentityModel.Tokens'] (https://github.com/AzureAD/azure-activedirectory- identitymodel-extensions-for-dotnet/blob/Master/src/Microsoft.IdentityModel.Tokens/TokenValidationParameters.cs) ..., które z jakiegoś powodu, jest ukryty w projekcie rozszerzenia AzureAD. Czyste jak błoto... – McGuireV10

Powiązane problemy