2017-08-22 15 views
11

Tôi đang cố di chuyển ứng dụng ASP.NET Core 1.1 lên ASP.NET Core 2.0.ASP.NET Core 2.0 HttpSys Xác thực Windows không thành công với thuộc tính Authorize (InvalidOperationException: Không xác thựcScheme được chỉ định)

Ứng dụng này khá đơn giản và bao gồm những điều sau đây:

  • Hosted trên HttpSys (trước đây là WebListener)
  • Sử dụng Windows authentication: options.Authentication.Schemes = AuthenticationSchemes.NTLM
  • Cho phép xác thực vô danh: options.Authentication.AllowAnonymous = true (vì có một số bộ điều khiển không yêu cầu xác thực)
  • Bộ điều khiển yêu cầu xác thực được trang trí với thuộc tính [Authorize].

Dự án biên dịch và khởi động tốt. Nó cũng phục vụ các hành động của bộ điều khiển không yêu cầu xác thực.

Tuy nhiên, ngay sau khi tôi nhấn một bộ điều khiển với các thuộc tính [Authorize], tôi nhận được ngoại lệ sau đây:

System.InvalidOperationException: No authenticationScheme was specified, 
and there was no DefaultChallengeScheme found. 
    at Microsoft.AspNetCore.Authentication.AuthenticationService.<ChallengeAsync>d__11.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at Microsoft.AspNetCore.Mvc.ChallengeResult.<ExecuteResultAsync>d__14.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeResultAsync>d__19.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeFilterPipelineAsync>d__17.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeAsync>d__15.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.<Invoke>d__7.MoveNext() 

tôi bắt đầu quan trọng xung quanh với các mẫu dự án và nhận thấy rằng tôi có thể tái sản xuất này dễ dàng sử dụng tiêu chuẩn mẫu Ứng dụng web lõi ASP.NET (Model-View-Controller) bằng Xác thực Windows.

tập tin Các Program.cs được thay đổi như sau:

public static IWebHost BuildWebHost(string[] args) => 
     WebHost.CreateDefaultBuilder(args) 
      .UseHttpSys(options => 
      { 
       options.Authentication.Schemes = AuthenticationSchemes.NTLM; 
       options.Authentication.AllowAnonymous = true; 
       options.MaxConnections = 100; 
       options.MaxRequestBodySize = 30000000; 
       options.UrlPrefixes.Add("http://localhost:5000"); 
      }) 
      .UseStartup<Startup>() 
      .Build(); 

này xuất phát trực tiếp từ HttpSys documentation. Ngoài ra tôi đã thêm thuộc tính [Authorize] vào lớp HomeController. Bây giờ, nó sẽ tạo ra chính xác cùng một ngoại lệ như được hiển thị.

Tôi đã tìm thấy một số bài đăng Stack Overflow có liên quan (here, herehere), nhưng không ai trong số họ giao dịch với Windows Authentication thuần túy (và câu trả lời dường như không khái quát).

Trả lời

12

Trong khi viết bài đăng, tôi nhớ đã xem qua số this subsection của hướng dẫn di chuyển. Nó nói để thêm

services.AddAuthentication(Microsoft.AspNetCore.Server.IISIntegration.IISDefaults.AuthenticationScheme); 

vào chức năng ConfigureServices.

Ban đầu tôi nghĩ rằng điều này sẽ không áp dụng cho HttpSys, được cung cấp tên đầy đủ của hằng số (đặc biệt là IISIntegration đã ném tôi đi). Hơn nữa, như của văn bản này, các HttpSys documentation hoàn toàn không đề cập đến điều này.

Đối với những người nhắm mục tiêu Khuôn khổ .NET đầy đủ, điều này yêu cầu cài đặt gói Microsoft.AspNetCore.Authentication NuGet.

EDIT

Như Tratcher chỉ ra, có một hằng số tương tự từ namespace HttpSys bạn chứ không nên sử dụng: Câu trả lời

Microsoft.AspNetCore.Server.HttpSys.HttpSysDefaults.AuthenticationScheme 
+0

ANH YÊU EM https://github.com/aspnet/CORS/issues/60#issuecomment-327484192 – Amivit

+2

Bạn có thể thay IISIntegration.IISDefaults với HttpSys.HttpSysDefaults, chúng xảy ra có cùng giá trị. – Tratcher

+0

Cảm ơn! Đã cập nhật câu trả lời. – Andreas

2

Andreas' đã cho tôi trên con đường đúng, nhưng đây là những gì đã làm việc cho tôi:

Đã thêm tham chiếu gói vào Microsoft.AspNetCore.Authentication

và sau đó cho Startup.cs

using Microsoft.AspNetCore.Server.IISIntegration;

public void ConfigureServices(IServiceCollection services) 
{ 
    ... 
    services.AddAuthentication(IISDefaults.AuthenticationScheme); 
    ... 
} 
Các vấn đề liên quan