In ASP.NET Core you can implement the IClaimsTransformation interface. This allows you to extend/change the incoming claimsprincipal:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ClaimsTransformer : IClaimsTransformation | |
{ | |
public Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal) | |
{ | |
((ClaimsIdentity)principal.Identity).AddClaim(new Claim("now", DateTime.Now.ToString())); | |
return Task.FromResult(principal); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddMvc(); | |
services.AddTransient<IClaimsTransformation, ClaimsTransformer>(); | |
} |
Unfortunately my ClaimsTransformer was never invoked when I used Windows Authentication in IIS.
The trick was to explicitly specify the IISServerDefaults.AuthenticationScheme:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddMvc(); | |
services.AddTransient<IClaimsTransformation, ClaimsTransformer>(); | |
services.AddAuthentication(IISServerDefaults.AuthenticationScheme); | |
} |