Configure ASP.NET core to work with windows authentication

.NET

To add windows authentication with IIS and an ASP.NET core application add this to the startup code:


builder.Services
   .AddAuthentication(NegotiateDefaults.AuthenticationScheme)
   .AddNegotiate();

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

Set windows authentication method in IIS

To get the authenticatated user use:


WindowsPrincipal winPrincipal = (WindowsPrincipal)User;

string username = winPrincipal.Identity.Name!.Split('\\').Last();

Notice, if you use WindowsIdentity.Current it returns the application pool user and not the authenticated user

Post a Comment

Previous Post Next Post