Antiforgery example #458
Replies: 3 comments 1 reply
-
Sounds about right to me. Also check out Saturn's implementation, the big difference there is a helper function for creating a form with the hidden input created for you. |
Beta Was this translation helpful? Give feedback.
-
Super minor but you shouldn't use let! isRequestValid = Async.AwaitTask(antiforgery.IsRequestValidAsync(ctx))
if isRequestValid then
...
else
... If you still want to keep it in line you can use match! Async.AwaitTask(antiforgery.IsRequestValidAsync(ctx)) with
| true -> ...
| false -> ... |
Beta Was this translation helpful? Give feedback.
-
This seems a good candidate to be added to the |
Beta Was this translation helpful? Give feedback.
-
Introduction
I finally added antiforgery protection to mvc-movie-giraffe.
I hadn't seen any examples of antiforgery with Giraffe yet so I thought I'd post here on how it can be setup.
Below I describe how it was setup for the form that is used to create new movie entries.
Setup
In
configureServices
, add the following line:Now it's available as a "service" that we can pull into various functions.
GET handler for create
In our
create_handler
, we'll pull in this service:Get and store the tokens (as well as hold on to the token set):
We'll pass the request token to the view so that it can use it in the form:
The entire
create_handler
:The view
OK, so now the
create
view accepts the request token:and uses it here:
When the form is submitted, the handler for the POST call will get invoked. Let's update that to check the request token.
POST handler for create
We pull in the antiforgery service:
We check to see if the request token is valid:
If it is, we continue on to add the movie. Otherwise, we return a 400 (bad request).
The entire handler is here.
Beta Was this translation helpful? Give feedback.
All reactions