【发布时间】:2020-03-23 12:23:31
【问题描述】:
我创建了一个带有 Windows 身份验证的服务器端 Blazor 应用程序。在 Visual Studio 2019 中使用 IIS Express 运行时,它工作正常并从数据库返回数据。
try
{
var filtered = _context.Trades
.Where(.....)
.ToListAsync(cancellationToken)
.ConfigureAwait(false);
// ....
}
catch (Exception ex)
{
// .... will display the exception message
}
然后我将其更改为 Kestrel。我预计会引发异常,因为我没有做任何事情来使 Kestrel 与 Windows 身份验证一起使用。
但是,它不会引发任何异常。在输出窗口中可以找到以下信息。
Microsoft.AspNetCore.Authorization.DefaultAuthorizationService:信息:授权失败。
如何确保它引发异常?
顺便说一句,如何让 Kestrel 与 Windows 身份验证一起工作?
Startup.cs 已更新答案。
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using BlazorApp1.Data;
using Microsoft.AspNetCore.Authentication.Negotiate;
namespace BlazorApp1
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddAuthentication(NegotiateDefaults.AuthenticationScheme).AddNegotiate();
services.AddSingleton<WeatherForecastService>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseAuthentication();
app.UseAuthorization();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
}
}
}
【问题讨论】:
-
你确定控制确实达到了这个语句吗?
-
是的,我设置了断点并逐行逐行
-
如何让 Kestrel 与 Windows 身份验证一起工作:docs.microsoft.com/en-us/aspnet/core/security/authentication/…
标签: c# iis windows-authentication blazor kestrel-http-server