Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate antiforgery tokens with IAntiforgery #1133

Merged
merged 1 commit into from
May 28, 2022

Conversation

ycanardeau
Copy link
Contributor

@ycanardeau ycanardeau commented May 14, 2022

@ycanardeau ycanardeau added security Security backend Data access layer (invisible to user) labels May 14, 2022
@ycanardeau ycanardeau mentioned this pull request May 14, 2022
11 tasks
Resources = new GlobalResources(this),
Values = new GlobalValues(this)
{
RequestToken = Antiforgery.GetAndStoreTokens(Context).RequestToken,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

React/Index.cshtml is the single page that will host the React app. The same token will be used multiple times, unless a full page refresh is triggered. See also javascript - Single Page Application and CSRF Token - Stack Overflow.

if (string.Equals(requestPath, "/", StringComparison.OrdinalIgnoreCase) || string.Equals(requestPath, "/index.html", StringComparison.OrdinalIgnoreCase))
{
var tokenSet = antiforgery.GetAndStoreTokens(context);
context.Response.Cookies.Append("XSRF-TOKEN", tokenSet.RequestToken!, new CookieOptions { HttpOnly = false });
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sets a cookie named XSRF-TOKEN. The client can read this cookie and provide its value as a header attached to AJAX requests. See Generate antiforgery tokens with IAntiforgery.

@ycanardeau ycanardeau merged commit 9be4834 into main May 28, 2022
@ycanardeau ycanardeau deleted the generate-antiforgery-tokens branch May 28, 2022 06:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend Data access layer (invisible to user) security Security
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant