skip to Main Content

I have this .NET core 6 project (cannot be upgraded to 7 atm) and it is an MVC project.
I am trying to figure out how on earth to use Windows authentication within Visual Studio using Kestrel. I cannot find much information.

using IIS works but this is a bit of a painful process to debug and set things up.

What needs to be done (using startup.cs) to get it to authenticate/prompt for Windows credentials?

This is the code (main parts) for startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllersWithViews()
        .AddFluentValidation(fv => fv.RegisterValidatorsFromAssemblyContaining<MyValidator>());
    services.RegisterServices(Configuration);
    services.ConfigureAppSettings(Configuration);
    services.ConfigureSmtpClient(Configuration);
    services.AddDataProtection()
        .SetApplicationName("MyApp")
        .PersistKeysToFileSystem(new DirectoryInfo(AppContext.BaseDirectory));
    services.AddDbContext<ITCIntranetDbContext>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("MyDB")));
    services.AddAuthentication(NegotiateDefaults.AuthenticationScheme);
}

Launchsettings.json:

{
  "profiles": {
    "MyProject.UI": {
      "commandName": "Project",
      "authenticationMode": "Windows",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "https://localhost:64991;http://localhost:64992"
    }
  },
  "iisSettings": {
    "windowsAuthentication": true,
    "anonymousAuthentication": false,
    "iisExpress": {
      "applicationUrl": "https://localhost:64991",
      "sslPort": 44308
    }
  }
}

Typical controller:

[Authorize]
public class MyController : Controller {...}

It just does not prompt. I get this error instead when I am needing to access resources that require authentication in the network:

InvalidOperationException: No authenticationScheme was specified, and there was no DefaultChallengeScheme found. The default schemes can be set using either AddAuthentication(string defaultScheme) or AddAuthentication(Action configureOptions). Microsoft.AspNetCore.Authentication.AuthenticationService.ChallengeAsync(HttpContext context, string scheme, AuthenticationProperties properties)

2

Answers


  1. Chosen as BEST ANSWER

    Interestingly, the missing piece was adding authorization as well

    app.UseAuthorization();


  2. Please follow this document , make sure you have the codes below :

        builder.Services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
           .AddNegotiate();
        
        builder.Services.AddAuthorization(options =>
        {
            options.FallbackPolicy = options.DefaultPolicy;
        });
    
    
    ......
      app.UseAuthorization();
    

    in .Net 6 ,there’re no startup.cs by default,If you keep it ,make sure you’ve called UseStartup method

    Also,you could select as below to create a prject sample with windows authentication:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search