What actually needed to be done... after a lucky research and the check your debug thrice:
program.cs needs to point to a custom authenticator with a custom scheme->
builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = "CustomWindowsAuthentication";
}).AddScheme<CustomWindowsAuthenticationOptions, CustomWindowsAuthenticationHandler>("CustomWindowsAuthentication", null);
Then you do indeed need an additional class that handles that stuff:
public class CustomWindowsAuthenticationHandler : AuthenticationHandler<CustomWindowsAuthenticationOptions>
{
public CustomWindowsAuthenticationHandler(
IOptionsMonitor<CustomWindowsAuthenticationOptions> options,
ILoggerFactory logger,
UrlEncoder encoder,
ISystemClock clock)
: base(options, logger, encoder, clock)
{
}
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
{
if (!Context.User.Identity.IsAuthenticated || !(Context.User.Identity is WindowsIdentity windowsIdentity))
{
return AuthenticateResult.NoResult();
}
var loginName = windowsIdentity.Name;
if (loginName.Contains("User1")
|| loginName.Contains("User2")
|| loginName.Contains("User3")
|| loginName.Contains("User4"))
{
var claims = new List<Claim>
{
new Claim("CustomClaim", "Admin")
};
var identity = new ClaimsIdentity(claims, Scheme.Name);
var principal = new ClaimsPrincipal(identity);
var ticket = new AuthenticationTicket(principal, Scheme.Name);
return AuthenticateResult.Success(ticket);
}
return AuthenticateResult.Fail("Custom authentication failed.");
}
}
And for the sake of completeness the "CustomWindowsAuthenticationOptions" because you need that too, although it's empty since i don't need any super special options.
public class CustomWindowsAuthenticationOptions : AuthenticationSchemeOptions
{
}