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.

Anti Forgery Claim Extractor: option to define claim name

See original GitHub issue

Background and Motivation

In a project that uses anti forgery features and 3rd party authentication provider, I found you can get anti forgery token validation failures in certain scenarios (auth provider does not use sub, nameidentifier or upn claims, whilst the iat claim value has changed).

If we could configure the name(s) of claims we want to use in the DefaultClaimUidExtractor class, we could fix the issue without having to change the configuration of the auth provider.

Proposed API


services.AddAntiForgery(options => 
    options.UidClaimNames = new string[] { "myUniqueUserClaim" }
);

Alternative Designs

Tell the framework that there is an iat claim, then update DefaultClaimUidExtractor to not include the claim in the extraction.


services.AddAntiForgery(options => 
    options.IatClaims = new string[] { "iat" }
);

Risks

Developers could configure a claim that is not unique to the user.

Issue Analytics

  • State:open
  • Created 8 months ago
  • Reactions:1
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
captainsafiacommented, Jan 31, 2023

Triage: This API would be a nice to have given more interest in it. Anyone interested in seeing this can give the issue a thumbs up to help us prioritize.

0reactions
lukecolbourncommented, Jan 25, 2023

Have you considered middleware that runs post-authentication and rewrites the ClaimsPrincipal to include a sub, nameidentifier or upn claim to match the myUniqueUserClaim?

Nice, that would sort it. I would definitely forget why that middleware exists and delete it in a couple of years though 😜 We’re getting the auth service admins to add/remove claims, saves us a few lines of code

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Anti-forgery token issues
Starting with MVC4 the anti-forgery-token doesn't use User.Identity.Name as the unique identifier. Instead it looks for the two claims given ...
Read more >
How to Include AntiForgeryToken for MVC Integration Testing
Once we do this, we can extract those properties from the HTML response by using the same names as declared in the AntiForgeryTokenExtractor ......
Read more >
XSRF/CSRF Prevention in ASP.NET MVC and Web Pages
Cross-site request forgery (also known as XSRF or CSRF) is an attack against web-hosted applications whereby a malicious web site can ...
Read more >
AntiForgeryToken: A Claim of Type NameIdentifier or ...
Name as anti-forgery token to validate form submitted. Worth to note that by default, ASP.NET MVC is not Claims-aware app.
Read more >
Claims-based authentication in MVC4 with .NET4.5 C# part 1
The incoming ClaimsPrincipal object is the outcome of the authentication; it represents the User that has just been authenticated on the login ...
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