Hi,
I'm writing a C# Module for DotNetNuke 9.13 and I'm having issues with the AntiForgeryToken. When I decorate the post method with [HttpPost] and [DotNetNuke.Web.Mvc.Framework.ActionFilters.ValidateAntiForgeryToken] I get an HTTP 401 error when I submit my form. If I remove the [DotNetNuke.Web.Mvc.Framework.ActionFilters.ValidateAntiForgeryToken] attribute then it works. When I check the page source BEFORE I click the submit button, I can see that the __RequestVerificationToken field inside the form, but when the form is posted, it is not present in the payload being sent (viewed using Fiddler).
There's nothing special with my cshtml form - the main body of it is this:
<code> < div id="PaymentDetails" class="paymentDetails"> < div> < label>@Dnn.LocalizeString("lblPaymentReference") *< /label> @Html.TextBoxFor(m => m.Details.Reference) < span>@Dnn.LocalizeString("lblPaymentReferenceExplanation")< /span> @Html.ValidationMessageFor(m => m.Details.Reference, @Dnn.LocalizeString("ReferenceRequired")) < /div> < /div> < div id="Actions" class="margins-tlr-15 padded"> < button type="submit" class="dnnPrimaryAction">@Dnn.LocalizeString("Submit")< /button> < /div></code>
My controller has the following method:
<code> [HttpPost] [DotNetNuke.Web.Mvc.Framework.ActionFilters.ValidateAntiForgeryToken] public ActionResult ManualPayment(DtoMakePayment makePayment) { return RedirectToDefaultRoute(); }</code>
When I remove the [DotNetNuke.Web.Mvc.Framework.ActionFilters.ValidateAntiForgeryToken] attribute - then my form posts correctly.
I've looked at the Dnn.ContactList.Mvc sample module - it looks to be implemented the same as what I have, except that my code is not working.
Any help would be greatly appreciated.
Thanx, Alon
Thanx for your response - muchn appreciated.
I'm using MVC with Razor script - not webforms. When I remove the AntiforgeryToken attribute - I can see that the corect methodin my controller is being called and everything is working, so I know that things are hooked up correctly. It just stops working when I put the attribute back on my method.
I'm currently not using JavaScript to submit the form - it is just a plain old submit button - using theDNN platform to submit.
The thing I don't understand is this:
I can see the hidden antifogery token in the form when I view the source code, but when I submit the form, I can see that the token is stripped from the payload being sent to the server (viewed in browser development tools).
Thanx,
Alon
Hi David,
Many thanks for responding to this post - I really appreciate it.
I based my code on the DnnSoftware MVC sample by Charles Nurse : https://github.com/dnnsof...Platform.Samples.Mvc
I don't see any obvious difference between my method and the one in the MVC sample - except that mine doesn't work and the sample does.
A bit more detail / information:
In both my module and the DnnSoftware MVC sample, the Edit form is a popup.
Upon further investigation, I could see that the parent page had the __RequestVerificationToken in a hidden field, but my popup page did not. When compared to the DNN MVC sample - that popup page did have the field in it.
To work around the issue, I simply created a hidden field and copied the token from the parent page.
This works, but i still don't understand why the field is missing in the first instance.
If you look at my original post - my method has the attribute decoration, but it still didn't work.
These Forums are for the discussion of the open source CMS DNN platform and ecosystem.
For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:
Awesome! Simply post in the forums using the link below and we'll get you started.