aspnetcore/security/authentication/windowsauth.md
By Rick Anderson and Kirk Larkin
:::moniker range=">= aspnetcore-6.0"
Windows Authentication (also known as Negotiate, Kerberos, or NTLM authentication) can be configured for ASP.NET Core apps hosted with IIS, Kestrel, or HTTP.sys.
Windows Authentication relies on the operating system to authenticate users of ASP.NET Core apps. Windows Authentication is used for servers that run on a corporate network using Active Directory domain identities or Windows accounts to identify users. Windows Authentication is best suited to intranet environments where users, client apps, and web servers belong to the same Windows domain.
Windows Authentication is suitable for web applications that operate within an organization's private internal network, accessible only to employees (and other authorized users) within the same network. The user management is done within Active Directory (AD), and users use their existing Windows domain account to authenticate.
Windows Authentication provides several benefits for intranet applications:
This makes Windows Authentication ideal for organizations that want to make use of their existing Windows infrastructure, such as intranet portals.
[!NOTE] Windows Authentication isn't supported with HTTP/2. While authentication challenges can be sent over HTTP/2 responses, the client must downgrade to HTTP/1.1 to complete the authentication process. This is a protocol limitation, not a deprecation of Windows Authentication. Once authenticated, normal HTTP/2 communication can resume for subsequent requests.
For public-facing applications Windows Authentication isn't recommended due to security and usability concerns. These reasons include:
Depending on your application requirements, consider these alternatives:
For public-facing applications:
For mixed environments with both intranet and external users:
For corporate environments using modern authentication:
Windows Authentication is a stateful scenario primarily used in an intranet, where a proxy or load balancer doesn't usually handle traffic between clients and servers. If a proxy or load balancer is used, Windows Authentication only works if the proxy or load balancer:
An alternative to Windows Authentication in environments where proxies and load balancers are used is Active Directory Federated Services (ADFS) with OpenID Connect (OIDC).
Add the Microsoft.AspNetCore.Authentication.Negotiate NuGet package and authentication services by calling xref:Microsoft.Extensions.DependencyInjection.AuthenticationServiceCollectionExtensions.AddAuthentication%2A in Program.cs:
The preceding code was generated by the ASP.NET Core Razor Pages template with Windows Authentication specified.
Configuration for launch settings only affects the Properties/launchSettings.json file for IIS Express and doesn't configure IIS for Windows Authentication. Server configuration is explained in the IIS section.
The Web Application templates available via Visual Studio or the .NET CLI can be configured to support Windows Authentication, which updates the Properties/launchSettings.json file automatically.
New project
Create a new Razor Pages or MVC app. In the Additional information dialog, set the Authentication type to Windows.
Run the app. The username appears in the rendered app's user interface.
Existing project
The project's properties enable Windows Authentication and disable Anonymous Authentication. Open the launch profiles dialog:
Alternatively, the properties can be configured in the iisSettings node of the launchSettings.json file:
New project
Execute the dotnet new command with the webapp argument (ASP.NET Core Web App) and --auth Windows switch:
dotnet new webapp --auth Windows
Existing project
Update the iisSettings node of the launchSettings.json file:
IIS uses the ASP.NET Core Module to host ASP.NET Core apps. Windows Authentication is configured for IIS via the web.config file. The following sections show how to:
If you haven't already done so, enable IIS to host ASP.NET Core apps. For more information, see xref:host-and-deploy/iis/index.
Enable the IIS Role Service for Windows Authentication. For more information, see Enable Windows Authentication in IIS Role Services (see Step 2).
IIS Integration Middleware is configured to automatically authenticate requests by default. For more information, see Host ASP.NET Core on Windows with IIS: IIS options (AutomaticAuthentication).
The ASP.NET Core Module is configured to forward the Windows Authentication token to the app by default. For more information, see ASP.NET Core Module configuration reference: Attributes of the aspNetCore element.
Use either of the following approaches:
Before publishing and deploying the project, add the following web.config file to the project root:
When the project is published by the .NET SDK (without the <IsTransformWebConfigDisabled> property set to true in the project file), the published web.config file includes the <location><security><authentication> section. For more information on the <IsTransformWebConfigDisabled> property, see xref:host-and-deploy/iis/web-config.
After publishing and deploying the project, perform server-side configuration with the IIS Manager:
When these actions are taken, IIS Manager modifies the app's web.config file. A <security><authentication> node is added with updated settings for anonymousAuthentication and windowsAuthentication:
The `` section added to the web.config file by IIS Manager is outside of the app's <location> section added by the .NET SDK when the app is published. Because the section is added outside of the <location> node, the settings are inherited by any sub-apps to the current app. To prevent inheritance, move the added <security> section inside of the <location> section that the .NET SDK provided.
When IIS Manager is used to add the IIS configuration, it only affects the app's web.config file on the server. A subsequent deployment of the app may overwrite the settings on the server if the server's copy of web.config is replaced by the project's web.config file. Use either of the following approaches to manage the settings:
The Microsoft.AspNetCore.Authentication.Negotiate NuGet package can be used with Kestrel to enable Windows Authentication using Negotiate and Kerberos on Windows, Linux, and macOS.
[!WARNING] Credentials can be persisted across requests on a connection. Negotiate authentication must not be used with proxies unless the proxy maintains a 1:1 connection affinity (a persistent connection) with Kestrel.
[!NOTE] The Negotiate handler detects if the underlying server supports Windows Authentication natively and if it is enabled. If the server supports Windows Authentication but it is disabled, an error is thrown asking you to enable the server implementation. When Windows Authentication is enabled in the server, the Negotiate handler transparently forwards authentication requests to it.
Authentication and a fallback authorization policy are enabled by the following highlighted code in Program.cs:
The preceding code was generated by the ASP.NET Core Razor Pages template with Windows Authentication specified.
Highlighted lines:
[!NOTE] Calling xref:Microsoft.Extensions.DependencyInjection.AuthenticationServiceCollectionExtensions.AddAuthentication%2A and xref:Microsoft.Extensions.DependencyInjection.NegotiateExtensions.AddNegotiate%2A registers and configures the Negotiate handler; it does not run authentication per request. The Authentication middleware (xref:Microsoft.AspNetCore.Builder.AuthAppBuilderExtensions.UseAuthentication%2A) invokes the handler and populates xref:Microsoft.AspNetCore.Http.HttpContext.User?displayProperty=nameWithType, and must appear before xref:Microsoft.AspNetCore.Builder.AuthorizationAppBuilderExtensions.UseAuthorization%2A for policy evaluation to work.
<a name="rbac"></a>
Kerberos authentication on Linux or macOS doesn't provide any role information for an authenticated user. To add role and group information to a Kerberos user, the authentication handler must be configured to retrieve the roles from an LDAP domain. The most basic configuration only specifies an LDAP domain to query against and uses the authenticated user's context to query the LDAP domain:
Some configurations may require specific credentials to query the LDAP domain. The credentials can be specified in the following highlighted options:
By default, the negotiate authentication handler resolves nested domains. In a large or complicated LDAP environment, resolving nested domains may result in a slow lookup or a lot of memory being used for each user. Nested domain resolution can be disabled using the IgnoreNestedGroups option.
Anonymous requests are allowed. Use ASP.NET Core Authorization to challenge anonymous requests for authentication.
The Microsoft.AspNetCore.Authentication.Negotiate API performs User Mode authentication. Service Principal Names (SPNs) must be added to the user account running the service, not the machine account. Execute setspn -S HTTP/myservername.mydomain.com myuser in an administrative command shell.
The Negotiate package on Kestrel for ASP.NET Core attempts to use Kerberos, which is a more secure and peformant authentication scheme than NTLM:
xref:Microsoft.AspNetCore.Authentication.Negotiate.NegotiateDefaults.AuthenticationScheme%2A?displayProperty=nameWithType specifies Kerberos because it's the default.
IIS, IISExpress, and Kestrel support both Kerberos and NTLM.
Examining the WWW-Authenticate: header using IIS or IISExpress with a tool like Fiddler shows either Negotiate or NTLM.
Kestrel only shows WWW-Authenticate: Negotiate
The WWW-Authenticate: Negotiate header means that the server can use NTLM or Kerberos. Kestrel requires the Negotiate header prefix, it doesn’t support directly specifying NTLM in the request or response auth headers. NTLM is supported in Kestrel, but it must be sent as Negotiate.
On Kestrel, to see if NTLM or Kerberos is used, Base64 decode the header and it shows either NTLM or HTTP. HTTP indicates Kerberos was used.
Instructions for joining a Linux or macOS machine to a Windows domain are available in the Connect Azure Data Studio to your SQL Server using Windows authentication - Kerberos article. The instructions create a machine account for the Linux machine on the domain. SPNs must be added to that machine account.
[!NOTE] When following the guidance in the Connect Azure Data Studio to your SQL Server using Windows authentication - Kerberos article, replace
python-software-propertieswithpython3-software-propertiesif needed.
Once the Linux or macOS machine is joined to the domain, additional steps are required to provide a keytab file with the SPNs:
setspn -S HTTP/mywebservice.mydomain.com mymachinesetspn -S HTTP/[email protected] mymachinektpass -princ HTTP/[email protected] -pass myKeyTabFilePassword -mapuser MYDOMAIN\mymachine$ -pType KRB5_NT_PRINCIPAL -out c:\temp\mymachine.HTTP.keytab -crypto AES256-SHA1export KRB5_KTNAME=/tmp/mymachine.HTTP.keytabklist to show the SPNs currently available for use.[!NOTE] A keytab file contains domain access credentials and must be protected accordingly.
HTTP.sys supports Kernel Mode Windows Authentication using Negotiate, NTLM, or Basic authentication.
The following code adds authentication and configures the app's web host to use HTTP.sys with Windows Authentication:
<!-- DOC AUTHOR NOTE The following hub.docker.com link is a valid URL, but the build system throws a broken link error because the page returns with various 400-series errors. Therefore, the link is code-fenced. -->[!NOTE] HTTP.sys delegates to Kernel Mode authentication with the Kerberos authentication protocol. User Mode authentication isn't supported with Kerberos and HTTP.sys. The machine account must be used to decrypt the Kerberos token/ticket that's obtained from Active Directory and forwarded by the client to the server to authenticate the user. Register the Service Principal Name (SPN) for the host, not the user of the app.
[!NOTE] HTTP.sys isn't supported on Nano Server version 1709 or later. To use Windows Authentication and HTTP.sys with Nano Server, use a Server Core (microsoft/windowsservercore) container (see
https://hub.docker.com/_/microsoft-windows-servercore). For more information on Server Core, see What is the Server Core installation option in Windows Server?.
The configuration state of anonymous access determines the way in which the [Authorize] and [AllowAnonymous] attributes are used in the app. The following two sections explain how to handle the disallowed and allowed configuration states of anonymous access.
When Windows Authentication is enabled and anonymous access is disabled, the [Authorize] and [AllowAnonymous] attributes have no effect. If an IIS site is configured to disallow anonymous access, the request never reaches the app. For this reason, the [AllowAnonymous] attribute isn't applicable.
When both Windows Authentication and anonymous access are enabled, use the [Authorize] and [AllowAnonymous] attributes. The [Authorize] attribute allows you to secure endpoints of the app which require authentication. The [AllowAnonymous] attribute overrides the [Authorize] attribute in apps that allow anonymous access. For attribute usage details, see xref:security/authorization/simple.
[!NOTE] By default, users who lack authorization to access a page are presented with an empty HTTP 403 response. The StatusCodePages Middleware can be configured to provide users with a better "Access Denied" experience.
ASP.NET Core doesn't implement impersonation. Apps run with the app's identity for all requests, using app pool or process identity. If the app should perform an action on behalf of a user, use xref:System.Security.Principal.WindowsIdentity.RunImpersonated%2A?displayProperty=nameWithType or xref:System.Security.Principal.WindowsIdentity.RunImpersonatedAsync%2A in a terminal inline middleware in Program.cs. Run a single action in this context and then close the context.
While the Microsoft.AspNetCore.Authentication.Negotiate package enables authentication on Windows, Linux, and macOS, impersonation is only supported on Windows.
When hosting with IIS, xref:Microsoft.AspNetCore.Authentication.AuthenticationService.AuthenticateAsync%2A isn't called internally to initialize a user. Therefore, an xref:Microsoft.AspNetCore.Authentication.IClaimsTransformation implementation used to transform claims after every authentication isn't activated by default. For more information and a code example that activates claims transformations, see Differences between in-process and out-of-process hosting.
:::moniker-end
:::moniker range="< aspnetcore-6.0"
Windows Authentication (also known as Negotiate, Kerberos, or NTLM authentication) can be configured for ASP.NET Core apps hosted with IIS, Kestrel, or HTTP.sys.
Windows Authentication relies on the operating system to authenticate users of ASP.NET Core apps. You can use Windows Authentication when your server runs on a corporate network using Active Directory domain identities or Windows accounts to identify users. Windows Authentication is best suited to intranet environments where users, client apps, and web servers belong to the same Windows domain.
Windows Authentication is suitable for web applications that operate within an organization's private internal network, accessible only to employees (and other authorized users) within the same network. The user management is done within Active Directory (AD), and users use their existing Windows domain account to authenticate.
Windows Authentication provides several benefits for intranet applications:
This makes Windows Authentication ideal for organizations that want to make use of their existing Windows infrastructure, such as intranet portals.
[!NOTE] Windows Authentication isn't supported with HTTP/2. While authentication challenges can be sent over HTTP/2 responses, the client must downgrade to HTTP/1.1 to complete the authentication process. This is a protocol limitation, not a deprecation of Windows Authentication. Once authenticated, normal HTTP/2 communication can resume for subsequent requests.
For public-facing applications Windows Authentication isn't recommended due to security and usability concerns. These reasons include:
Depending on your application requirements, consider these alternatives:
For public-facing applications:
For mixed environments with both intranet and external users:
For corporate environments using modern authentication:
Windows Authentication is a stateful scenario primarily used in an intranet, where a proxy or load balancer doesn't usually handle traffic between clients and servers. If a proxy or load balancer is used, Windows Authentication only works if the proxy or load balancer:
An alternative to Windows Authentication in environments where proxies and load balancers are used is Active Directory Federated Services (ADFS) with OpenID Connect (OIDC).
Add authentication services by invoking xref:Microsoft.Extensions.DependencyInjection.AuthenticationServiceCollectionExtensions.AddAuthentication%2A (xref:Microsoft.AspNetCore.Server.IISIntegration?displayProperty=fullName namespace) in Startup.ConfigureServices:
services.AddAuthentication(IISDefaults.AuthenticationScheme);
Configuration for launch settings only affects the Properties/launchSettings.json file for IIS Express and doesn't configure IIS for Windows Authentication. Server configuration is explained in the IIS section.
The Web Application template available via Visual Studio or the .NET CLI can be configured to support Windows Authentication, which updates the Properties/launchSettings.json file automatically.
New project
Run the app. The username appears in the rendered app's user interface.
Existing project
The project's properties enable Windows Authentication and disable Anonymous Authentication:
Alternatively, the properties can be configured in the iisSettings node of the launchSettings.json file:
New project
Execute the dotnet new command with the webapp argument (ASP.NET Core Web App) and --auth Windows switch:
dotnet new webapp --auth Windows
Existing project
Update the iisSettings node of the launchSettings.json file:
When modifying an existing project, confirm that the project file includes a package reference for the Microsoft.AspNetCore.App metapackage or the Microsoft.AspNetCore.Authentication NuGet package.
IIS uses the ASP.NET Core Module to host ASP.NET Core apps. Windows Authentication is configured for IIS via the web.config file. The following sections show how to:
If you haven't already done so, enable IIS to host ASP.NET Core apps. For more information, see xref:host-and-deploy/iis/index.
Enable the IIS Role Service for Windows Authentication. For more information, see Enable Windows Authentication in IIS Role Services (see Step 2).
IIS Integration Middleware is configured to automatically authenticate requests by default. For more information, see Host ASP.NET Core on Windows with IIS: IIS options (AutomaticAuthentication).
The ASP.NET Core Module is configured to forward the Windows Authentication token to the app by default. For more information, see ASP.NET Core Module configuration reference: Attributes of the aspNetCore element.
Use either of the following approaches:
Before publishing and deploying the project, add the following web.config file to the project root:
When the project is published by the .NET SDK (without the <IsTransformWebConfigDisabled> property set to true in the project file), the published web.config file includes the <location><security><authentication> section. For more information on the <IsTransformWebConfigDisabled> property, see xref:host-and-deploy/iis/index#webconfig-file.
After publishing and deploying the project, perform server-side configuration with the IIS Manager:
When these actions are taken, IIS Manager modifies the app's web.config file. A <security><authentication> node is added with updated settings for anonymousAuthentication and windowsAuthentication:
The `` section added to the web.config file by IIS Manager is outside of the app's <location> section added by the .NET SDK when the app is published. Because the section is added outside of the <location> node, the settings are inherited by any sub-apps to the current app. To prevent inheritance, move the added <security> section inside of the <location> section that the .NET SDK provided.
When IIS Manager is used to add the IIS configuration, it only affects the app's web.config file on the server. A subsequent deployment of the app may overwrite the settings on the server if the server's copy of web.config is replaced by the project's web.config file. Use either of the following approaches to manage the settings:
The Microsoft.AspNetCore.Authentication.Negotiate NuGet package can be used with Kestrel to support Windows Authentication using Negotiate and Kerberos on Windows, Linux, and macOS.
[!WARNING] Credentials can be persisted across requests on a connection. Negotiate authentication must not be used with proxies unless the proxy maintains a 1:1 connection affinity (a persistent connection) with Kestrel.
[!NOTE] The Negotiate handler detects if the underlying server supports Windows Authentication natively and if it is enabled. If the server supports Windows Authentication but it is disabled, an error is thrown asking you to enable the server implementation. When Windows Authentication is enabled in the server, the Negotiate handler transparently forwards authentication requests to it.
Add authentication services by invoking xref:Microsoft.Extensions.DependencyInjection.AuthenticationServiceCollectionExtensions.AddAuthentication%2A and xref:Microsoft.Extensions.DependencyInjection.NegotiateExtensions.AddNegotiate%2A in Startup.ConfigureServices:
// using Microsoft.AspNetCore.Authentication.Negotiate;
// using Microsoft.Extensions.DependencyInjection;
services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
.AddNegotiate();
Add Authentication Middleware by calling xref:Microsoft.AspNetCore.Builder.AuthAppBuilderExtensions.UseAuthentication%2A in Startup.Configure:
app.UseAuthentication();
For more information on middleware, see xref:fundamentals/middleware/index.
<a name="rbac"></a>
Kerberos authentication on Linux or macOS doesn't provide any role information for an authenticated user. To add role and group information to a Kerberos user, the authentication handler must be configured to retrieve the roles from an LDAP domain. The most basic configuration only specifies an LDAP domain to query against and will use the authenticated user's context to query the LDAP domain:
services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
.AddNegotiate(options =>
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
options.EnableLdap("contoso.com");
}
});
Some configurations may require specific credentials to query the LDAP domain. The credentials can be specified in the following highlighted options:
By default, the negotiate authentication handler resolves nested domains. In a large or complicated LDAP environment, resolving nested domains may result in a slow lookup or a lot of memory being used for each user. Nested domain resolution can be disabled using the IgnoreNestedGroups option.
Anonymous requests are allowed. Use ASP.NET Core Authorization to challenge anonymous requests for authentication.
xref:Microsoft.AspNetCore.Authentication.Negotiate.NegotiateDefaults.AuthenticationScheme requires the Microsoft.AspNetCore.Authentication.Negotiate NuGet package.
The Microsoft.AspNetCore.Authentication.Negotiate API performs User Mode authentication. Service Principal Names (SPNs) must be added to the user account running the service, not the machine account. Execute setspn -S HTTP/myservername.mydomain.com myuser in an administrative command shell.
Instructions for joining a Linux or macOS machine to a Windows domain are available in the Connect Azure Data Studio to your SQL Server using Windows authentication - Kerberos article. The instructions create a machine account for the Linux machine on the domain. SPNs must be added to that machine account.
[!NOTE] When following the guidance in the Connect Azure Data Studio to your SQL Server using Windows authentication - Kerberos article, replace
python-software-propertieswithpython3-software-propertiesif needed.
Once the Linux or macOS machine is joined to the domain, additional steps are required to provide a keytab file with the SPNs:
setspn -S HTTP/mywebservice.mydomain.com mymachinesetspn -S HTTP/[email protected] mymachinektpass -princ HTTP/[email protected] -pass myKeyTabFilePassword -mapuser MYDOMAIN\mymachine$ -pType KRB5_NT_PRINCIPAL -out c:\temp\mymachine.HTTP.keytab -crypto AES256-SHA1export KRB5_KTNAME=/tmp/mymachine.HTTP.keytabklist to show the SPNs currently available for use.[!NOTE] A keytab file contains domain access credentials and must be protected accordingly.
HTTP.sys supports Kernel Mode Windows Authentication using Negotiate, NTLM, or Basic authentication.
Add authentication services by invoking xref:Microsoft.Extensions.DependencyInjection.AuthenticationServiceCollectionExtensions.AddAuthentication%2A (xref:Microsoft.AspNetCore.Server.HttpSys?displayProperty=fullName namespace) in Startup.ConfigureServices:
services.AddAuthentication(HttpSysDefaults.AuthenticationScheme);
Configure the app's web host to use HTTP.sys with Windows Authentication (Program.cs). xref:Microsoft.AspNetCore.Hosting.WebHostBuilderHttpSysExtensions.UseHttpSys%2A is in the xref:Microsoft.AspNetCore.Server.HttpSys?displayProperty=fullName namespace.
<!-- DOC AUTHOR NOTE The following hub.docker.com link is a valid URL, but the build system throws a broken link error because the page returns with various 400-series errors. Therefore, the link is code-fenced. -->[!NOTE] HTTP.sys delegates to Kernel Mode authentication with the Kerberos authentication protocol. User Mode authentication isn't supported with Kerberos and HTTP.sys. The machine account must be used to decrypt the Kerberos token/ticket that's obtained from Active Directory and forwarded by the client to the server to authenticate the user. Register the Service Principal Name (SPN) for the host, not the user of the app.
[!NOTE] HTTP.sys isn't supported on Nano Server version 1709 or later. To use Windows Authentication and HTTP.sys with Nano Server, use a Server Core (microsoft/windowsservercore) container (see
https://hub.docker.com/_/microsoft-windows-servercore). For more information on Server Core, see What is the Server Core installation option in Windows Server?.
The configuration state of anonymous access determines the way in which the [Authorize] and [AllowAnonymous] attributes are used in the app. The following two sections explain how to handle the disallowed and allowed configuration states of anonymous access.
When Windows Authentication is enabled and anonymous access is disabled, the [Authorize] and [AllowAnonymous] attributes have no effect. If an IIS site is configured to disallow anonymous access, the request never reaches the app. For this reason, the [AllowAnonymous] attribute isn't applicable.
When both Windows Authentication and anonymous access are enabled, use the [Authorize] and [AllowAnonymous] attributes. The [Authorize] attribute allows you to secure endpoints of the app which require authentication. The [AllowAnonymous] attribute overrides the [Authorize] attribute in apps that allow anonymous access. For attribute usage details, see xref:security/authorization/simple.
[!NOTE] By default, users who lack authorization to access a page are presented with an empty HTTP 403 response. The StatusCodePages Middleware can be configured to provide users with a better "Access Denied" experience.
ASP.NET Core doesn't implement impersonation. Apps run with the app's identity for all requests, using app pool or process identity. If the app should perform an action on behalf of a user, use WindowsIdentity.RunImpersonated or xref:System.Security.Principal.WindowsIdentity.RunImpersonatedAsync%2A in a terminal inline middleware in Startup.Configure. Run a single action in this context and then close the context.
While the Microsoft.AspNetCore.Authentication.Negotiate package enables authentication on Windows, Linux, and macOS, impersonation is only supported on Windows.
When hosting with IIS, xref:Microsoft.AspNetCore.Authentication.AuthenticationService.AuthenticateAsync%2A isn't called internally to initialize a user. Therefore, an xref:Microsoft.AspNetCore.Authentication.IClaimsTransformation implementation used to transform claims after every authentication isn't activated by default. For more information and a code example that activates claims transformations, see Differences between in-process and out-of-process hosting.
:::moniker-end