0

I'm trying to set up an MVC Core Web App with Windows Authentication in Visual Studio 2022 but I can't get it to work.

I create a new project and select the Windows Authentication option. I immediately try to run the app but I get a blank page.

For troubleshooting I then added the following else clause so I can see what the problem is on my development machine.

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();
}

and I can then see that I have a '401 Unauthorised' status code. And then if I add [AllowAnonymous] to my Index action I can finally see the home page but my windows username is not displayed. I would expect to see 'Hello username' displayed in the top right but I don't seem to be authenticated, let alone authorized.

Apart from the two troubleshooting steps above, this is a brand new project straight out of the box but I've pasted my Program.cs below for reference.

What do I need to do to get Windows Authentication to work?

Thanks

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();

1 Answers1

0

You can follow my steps to fix the issue.

  1. Enable IIS features in your develop machine, this just want to enable Windows authentication on Windows.

    enter image description here

    Tips: Please expand all the fields and tick them, then click Apply.

    enter image description here

  2. Delete the .vs folder inside your project, this steps just want to reset the applicationhost.config.

  3. The latest step, we need to double check the settings file in project.

    enter image description here

Then the issue should be fixed now.

Jason Pan
  • 15,263
  • 1
  • 14
  • 29
  • 1
    Thanks Jason. This didn't exactly fix my problem but it did point me in the right direction. I was using the Kestrel web server as that's the default, rather than IIS. When I selected IIS instead, everything worked as expected. I wonder if it's possible to configure Kestrel to use Windows Authentication but I don't actually need to do that so maybe that's a question for another thread. Thanks again - problem solved. – lonelyoutpost Nov 29 '22 at 07:36
  • @lonelyoutpost Just now I test in my local and use Kestrel, it works well and I don't know why. If you are intresting about this issue, you can check [this thread](https://stackoverflow.com/questions/53842990/windows-authentication-for-kestrel-hosted-in-windows-service). – Jason Pan Nov 29 '22 at 09:44
  • It may be something to do with the fact that I was in a domain environment, at work. When I try from home, with Kestrel, it does work. Anyway, thanks for the link. I'll check it out. Thanks for coming back to me. – lonelyoutpost Nov 29 '22 at 18:17
  • 1
    I discovered that this issue is unique to Firefox. When I run a Windows Authentication app in Chrome, with Kestrel, it works fine! – lonelyoutpost Feb 07 '23 at 18:29
  • 1
    I've found the solution to this [thanks to Rich Strahl](https://weblog.west-wind.com/posts/2019/Nov/14/FireFox-Windows-Security-and-Kestrel-on-ASPNET-Core) – lonelyoutpost Feb 10 '23 at 07:59