[英]MVC Core Windows Authentication not working
我正在嘗試在 Visual Studio 2022 中設置具有 Windows 身份驗證的 MVC Core Web 應用程序,但我無法使其正常工作。
我創建了一個新項目和 select Windows 身份驗證選項。 我立即嘗試運行該應用程序,但我得到一個空白頁面。
為了進行故障排除,我添加了以下 else 子句,以便我可以查看我的開發機器上的問題所在。
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/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();
}
else
{
// Development Environment
app.UseStatusCodePages();
}
然后我可以看到我有一個“401 Unauthorised”狀態代碼。 然后,如果我將 [AllowAnonymous] 添加到我的索引操作中,我終於可以看到主頁,但我的 windows 用戶名沒有顯示。 我希望看到右上角顯示“你好用戶名”,但我似乎沒有經過身份驗證,更不用說授權了。
除了上面的兩個故障排除步驟之外,這是一個開箱即用的全新項目,但我在下面粘貼了我的 Program.cs 以供參考。
我需要做什么才能使 Windows 身份驗證正常工作?
謝謝
using Microsoft.AspNetCore.Authentication.Negotiate;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
.AddNegotiate();
builder.Services.AddAuthorization(options =>
{
// By default, all incoming requests will be authorized according to the default policy.
options.FallbackPolicy = options.DefaultPolicy;
});
builder.Services.AddRazorPages();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/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();
}
else
{
// Development Environment
app.UseStatusCodePages();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.