Category / Section
How to use AntiForgeryToken with uploadbox
1 min read
Using AntiForgeryToken with uploadbox
Use the AntiForgeryToken() helper class to use it with the uploadbox. First, define the uploadbox control and then, call the AntiForgeryToken helper class to add the antiforgery token in the form. Refer to the following code.
@using (Html.BeginForm("ImportFile", "FileUpload", FormMethod.Post, new { id = "frmUpload", enctype ="multipart/form-data" })) { @Html.AntiForgeryToken() @Html.EJ().Uploadbox("UploadDefault").SaveUrl("SaveDefault").RemoveUrl("RemoveDefault").UploadBoxDialogAction(da=>da.CloseOnComplete(true)) <input type="submit" id="btnUpload" value="Submit" onclick="onComplete(this)" />}
Then, in the form submit, validate the AntiForgeryToken using the ValidateAntiForgeryToken attribute in the server-side. The token value can be get from the FormCollection data. Refer to the following code.
[HttpPost] [ValidateAntiForgeryToken] public ActionResult ImportFile(FormCollection fc) { string token = fc["__RequestVerificationToken"]; return View("FileUploadFeatures"); }