授权失败。不会引发异常?

问题描述 投票:0回答:1

我使用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");
            });
        }
    }
}
c# iis windows-authentication blazor kestrel-http-server
1个回答
0
投票

如何确保引发异常?

如果为IIS启用了Windows身份验证,则请求将不会出现在代码中,IIS将拒绝该请求,因此您将不会收到任何异常消息。要对此进行检查,您应该检查IIS日志。

顺便说一句,如何使Kestrel与Windows身份验证兼容?

根据您的描述,我想您只是为IIS而不是Kestrel启用了Windows身份验证。因此,如果您直接从Kestrel运行应用程序,它将不会使用Windows身份验证。

为了解决此问题,我建议您可以按照以下步骤为asp.net核心应用程序启用Windows身份验证。

1。安装Microsoft.AspNetCore.Authentication.Negotiate程序包

  1. 将以下代码添加到Startup.cs ConfigureServices方法中

         services.AddAuthentication(NegotiateDefaults.AuthenticationScheme).AddNegotiate();
    
  2. 将以下代码添加到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>
© www.soinside.com 2019 - 2024. All rights reserved.