【发布时间】: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