2017-06-14 53 views
6

我試圖在不使用身份的情況下進行身份驗證。我發現了一些文章,描述瞭如何在其他版本中使用它,但是對於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的貶值,並沒有發現任何解決方案

+1

我沒有設置任何嘗試2.0有關,但我對你的問題非常感興趣,因爲它會影響我。如果你看看他們的一些單元測試,你至少可以知道你應該調用什麼對象:https://github.com/aspnet/Identity/pull/1119/commits/2bdfdb3220b3aca7513455652617af5685d5293c –

回答

5

它看起來像有與驗證2.0(https://github.com/aspnet/Announcements/issues/232

的設置是正確的,但是我需要一些重大的變動做兩件事情:

  1. 使用HttpContext.SignInAsync()using Microsoft.AspNetCore.Authentication),而不是HttpContext.Authentication.SignInAsync()
  2. 使用"AuthenticationTypes.Federation"作爲身份驗證類型 (注意:其他值似乎不起作用,空白將導致用戶名被設置並且IsAuthenticated爲false) var identity = new ClaimsIdentity(claims, "AuthenticationTypes.Federation");

下面是更正後的代碼

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

在控制器

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

而不是難 - 編碼'AuthenticationTypes.Federation',你應該引用'TokenValidationParameters.DefaultAuthenticationType',它被硬編碼爲['Microsoft.IdentityModel.Tokens']中的值(https://github.com/AzureAD/azure-activedirectory- identitymodel-extensions-for-dotnet/blob/master/src/Microsoft.IdentityModel.Tokens/TokenValidationParameters.cs)......出於某種原因,它隱藏在AzureAD擴展項目中。清除泥土... – McGuireV10