【问题标题】:Enable windows auth for Kestrel on NET5 asp.net core webapp在 NET 5 asp.net 核心 Web 应用程序中为 Kestrel 启用 Windows 身份验证
【发布时间】:2021-10-15 04:27:48
【问题描述】:

我在 Visual Studio 2019 中从模板 ASP.NET Core Web 应用程序(目标框架:.NET 5.0(当前))创建了测试应用程序。

现在我想设置这个应用程序以使用 Windows 身份验证,如下所述:https://docs.microsoft.com/en-us/aspnet/core/security/authentication/windowsauth?view=aspnetcore-5.0&tabs=visual-studio#kestrel

  • 我已经安装了 nuget 包
  • 我在 Startup.ConfigureServices 中添加了services.AddAuthentication(NegotiateDefaults.AuthenticationScheme).AddNegotiate();
  • 我在UseAuthorization 之前添加了app.UseAuthentication();,如ASP.NET Core Middleware 中所述

我还更正了 index.cshtml 以指示登录的用户名:

    <h1 class="display-4">Welcome, @HttpContext.User.Identity?.Name</h1>

但是当我将此站点发布到文件夹(目标运行时:便携式)并复制到安装了 NET5 的远程 Windows 服务器(域中的服务器,当然是 Windows Server 2012)时 - 我在浏览网站时看不到用户名。 (我从命令行运行 WebApplication2.exe)

我的情况出了什么问题,或者我应该检查什么来解决这个问题?

PS 我的 Startup.cs:

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.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddRazorPages();

        services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
                .AddNegotiate();
    }

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

        app.UseStaticFiles();

        app.UseRouting();

        app.UseAuthentication();
        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapRazorPages();
        });
    }
}

这适用于我自己的工作站,它也是同一域的一部分。即使没有安装这个 nuget 包(在 IIS 模式下),它也会显示我的用户名。

我不仅检查了 chrome,还检查了 IE 和 Firefox - 结果相同。

我已经检查过该协议不是 HTTP/2:


当我将此字符串添加到我的 appsettings.json 时:

  "Microsoft.AspNetCore.Authentication": "Debug"

然后我可以在控制台日志中看到这条消息:

dbug: Microsoft.AspNetCore.Authentication.Negotiate.NegotiateHandler[9]
      AuthenticationScheme: Negotiate was not authenticated.

【问题讨论】:

    标签: c# asp.net-core authentication .net-5 kestrel-http-server


    【解决方案1】:

    aspnetcore sources on github 中有一个working example。它也适用于我的服务器。

    所以,在逐行比较两个解决方案后,我发现了不同之处。

    除了官方文档告诉您需要在 Startup.cs 中添加这一行:

            services.AddAuthorization(options =>
            {
                options.FallbackPolicy = options.DefaultPolicy;
            });
    

    您需要指定 DefaultPolicy,因为它包含 .RequireAuthenticatedUser() 调用:

    public AuthorizationPolicy DefaultPolicy { get; set; } = new AuthorizationPolicyBuilder(Array.Empty<string>()).RequireAuthenticatedUser().Build();
    

    如果你只添加 services.AddAuthorization(); 这将不起作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      相关资源
      最近更新 更多