question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

IIS: Using Windows Authentication assigns ClaimsPrincipal to User if AuthenticationSchemes are defined in an AuthorizationPolicy

See original GitHub issue

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

When running under IIS / IIS Express with Windows Authentication enabled, defining an AuthorizationPolicy with AuthenticationSchemes will assign a ClaimsPrincipal to the User of the current request instead of a WindowsPrincipal.

In the past (.NET Core 2.1), I’ve unknowingly worked around this by returning an empty string array into the authenticationScheme parameter in the AuthorizationPolicy constructor, but I’ve noticed that simply adding any existing defined authentication scheme to the parameter - including “Windows” - will result in a ClaimsPrincipal assigned to HttpContext.User instead of a WindowsPrincipal.

This will return a WindowsPrincipal when HttpContext.User or ControllerBase.User is called.

builder.Services.AddAuthorization(pol =>
 {
     pol.AddPolicy("WindowsAuth",
         new AuthorizationPolicy(
             new[] {new SomeRequirement() }, 
             Enumerable.Empty<string>());
 });

This will return a ClaimsPrincipal when HttpContext.User or ControllerBase.User is called.

builder.Services.AddAuthorization(pol =>
 {
     pol.AddPolicy("WindowsAuth",
         new AuthorizationPolicy(
             new[] {new SomeRequirement() }, 
             new string[] {"Windows"});
 });

I’m currently experiencing this in .NET Core 3.1, but realized that it also affects .NET 6 (and probably 5)

Expected Behavior

If “Windows” is passed into the set of AuthenticationSchemes an AuthorizationPolicy should affect, a WindowsPrincipal should be assigned to the User of the current request instead of a ClaimsPrincipal.

Steps To Reproduce

Repro Repo: https://github.com/snickler/authpolicyrepro

Exceptions (if any)

No response

.NET Version

6.0.101

Anything else?

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
HaoKcommented, Feb 4, 2022

Only one, if you specify multiple schemes, they get combined into one ClaimsPrincipal, basically don’t mix windows auth with other auth schemes if you want to get a Windows Principal

1reaction
HaoKcommented, Feb 4, 2022
Read more comments on GitHub >

github_iconTop Results From Across the Web

Migrate authentication and Identity to ASP.NET Core 2.0
This article outlines the most common steps for migrating ASP.NET Core 1.x authentication and Identity to ASP.NET Core 2.0.
Read more >
How can I configure IIS to use ClaimsIdentity instead of ...
I'm using Azure AD to authenticate the user and emit roles according to the security groups to which the user is assigned. The...
Read more >
Overview of ASP.NET Core Authentication
Authentication is responsible for providing the ClaimsPrincipal for authorization to make permission decisions against. There are multiple ...
Read more >
Policy-based authorization in ASP.NET Core
Learn how to create and use authorization policy handlers for enforcing authorization requirements in an ASP.NET Core app.
Read more >
Configure Windows Authentication in ASP.NET Core
Select Anonymous Authentication. Select Disable in the Actions sidebar. Select Windows Authentication. Select Enable in the Actions sidebar.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found