c# - 授权失败。不引发异常?
问题描述
我创建了一个带有 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 与 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");
});
}
}
}
解决方案
如何确保它引发异常?
如果您为 IIS 启用了 windows auth,请求将不会到达您的代码,IIS 将拒绝该请求,因此您不会收到任何异常消息。要检查这一点,您应该检查 IIS 日志。
顺便说一句,如何使 Kestrel 与 Windows 身份验证一起使用?
根据您的描述,我猜您只是为 IIS 而不是 Kestrel 启用了 Windows 身份验证。因此,如果您直接从 Kestrel 运行应用程序,它将不会使用 windows auth。
要解决此问题,我建议您可以按照以下步骤为您的 asp.net 核心应用程序启用 Windows 身份验证。
1.安装Microsoft.AspNetCore.Authentication.Negotiate包
将以下代码添加到 Startup.cs ConfigureServices 方法中
services.AddAuthentication(NegotiateDefaults.AuthenticationScheme).AddNegotiate();
将以下代码添加到 Startup.cs 配置方法中
app.UseAuthentication(); app.UseAuthorization();
4.然后您可以使用 dotnet run 来测试您的应用程序。您会发现将为您的应用程序启用 Windows 身份验证。
如果您在 Kestrel 中使用了 Windows 身份验证,则需要强制页面只允许用户登录。为此,我建议您可以在 _Host.cshtml 中添加 Authorize 属性。
细节,你可以参考下面的_Host.cshtml。
@page "/"
@using Microsoft.AspNetCore.Authorization
@attribute [Authorize]
@namespace BlazorWindowsAuth.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>BlazorWindowsAuth</title>
<base href="~/" />
<link rel="stylesheet" href="css/bootstrap/bootstrap.min.css" />
<link href="css/site.css" rel="stylesheet" />
</head>
<body>
<app>
@(await Html.RenderComponentAsync<App>(RenderMode.ServerPrerendered))
</app>
<script src="_framework/blazor.server.js"></script>
</body>
</html>
推荐阅读
- solidity - 无法构造包含(嵌套)映射的结构。坚固
- django - 为什么 Django 模型扩展验证器不起作用
- javascript - 如何获取我的电子邮件并在我的 React APP 中显示它们
- python - 'npm install' 命令未成功执行,引发一些 python 错误
- ada - 禁止未经检查的转换的警告
- android - 将一个模块导入到我的 Flutter 应用程序中,但如何使用它?
- r - 变异里面的公式和使用~
- node.js - 处理错误 mongoDB
- php - Mysql WHERE 子句中的多个参数导致问题
- php - 如何在 Shopware 6 插件 ZIP 中包含重叠的作曲家依赖项?