antiforgery stopping basic razor page post with 400 error - asp.net-core

I have been getting a 400 error, and cut the code down to a very basic form post, and still get this error.
It happens once only on first attempt (i.e. first time page is loaded ever, then, go back, press button, and all ok)
I have found that it is related to "antiforgery", and if i disable this, all works perfect first time
<div class="text-center">
<form method="post">
<button id="btnLogin" type="submit" asp-page-handler="BtnSignin">Login</button>
</form>
The above is my HTML, and below is the c#:
public async Task<IActionResult> OnPostBtnSignin()
{
return null;
}
(i know returning null isnt ideal, but, for my debugging, it confirms if i get the 400 error or not)
if I publish (to aws lambda), on first button press, it errors, go back, press button, and this time, no error
if i disable "antiforgery", it works (have tried, and its consistent either way round)
Any ideas on why? and how to fix? (without disabling antiforgery)

Try to add #Html.AntiForgeryToken() to your form:
<form method="post">
#Html.AntiForgeryToken()
<button id="btnLogin" type="submit" asp-page-handler="BtnSignin">Login</button>
</form>
Both <form method="post"></form> and #Html.AntiForgeryToken() will result in the __RequestVerificationToken hidden field being added to the page.So if your form cannot add __RequestVerificationToken to page,you can try to add #Html.AntiForgeryToken().

Related

Refreshing Html.Action

I have a partial view rendered with an Html.Action() that I want to refresh on a button click. I've tried AJAX requests, but the data I'm passing back and forth exceeds the maximum length for JSON.
The basic structure of the page looks like:
<html>
<head>
...
</head>
<body>
<div>
#Html.Action("DisplayBox")
</div>
<div>
<input type="button" id="RefreshButton" value="Refresh Box" />
</div>
</body>
</html>
The reason why I'm asking for a method other than an AJAX request is that the partial I'm rendering is a PDF object:
#model byte[]
#{
String base64EncodedPDF = System.Convert.ToBase64String(Model);
Layout = null;
}
<object data="data:application/pdf;base64,#base64EncodedPDF"
width="900" height="900" type="application/pdf"></object>
Thus, the data passed to the partial view for rendering is too big to put in an AJAX request. On button click, I want to be able to execute the controller action and have the results update the partial with new data. Is there any way of doing this?
You have to load the HTML with the link to the controller that generate the PDF or generate the file on the server side, host it and return the URL of this PDF, then, javascript can redirect user to that file.
I don't think that returning file trough AJAX is really not a good practice!

Google script HTML api can´t process file upload field

I am trying to create a simple web app in google scripts with the HTML api.
code.gs
function doGet() {
return HtmlService.createHtmlOutputFromFile('index');
}
function processForm(formObject) {
var formBlob = formObject.myFile;
var driveFile = DriveApp.createFile(formBlob);
return driveFile.getUrl();
}
index.html
<script>
function updateUrl(url) {
var div = document.getElementById('output');
div.innerHTML = 'Got it!';
}
</script>
<form id="myForm">
<input name="myFile" type="file" />
<input type="button" value="Submit"
onclick="google.script.run
.withSuccessHandler(updateUrl)
.processForm(this.parentNode)" />
</form>
<div id="output"></div>
The form fails to submit. I´m using google chrome Versión 30.0.1599.101 m
This appears in the console: Uncaught NetworkError: Form submission failed.
Here is the app: https://script.google.com/d/1yrgM20n1ZI99bChN2qtQWgGck36OccLN3A16Gn7tCPvsJw0EcK_ql7C5/edit?usp=sharing
Maybe you should add encoding="multipart/form-data" attribute to form tag.
If not solved already – did you try changing the input type from "button" to "submit"? On top of that I'd also try giving it another value than "Submit" since that might interfere with the actual submit parameter.

VB.net clicking a submit button using Web Browser

I want to click the submit button of the webpage i am displaying in my Web Browser but it seems not to work at all here is my code:
WebBrowser1.Document.Forms(0).InvokeMember("image")
i was basing on the html code of the button i want to click which is:
<div class="buttonRow forward">
<input type="image" src="includes/templates/template_default/buttons/english/button_send.gif" alt="Send Now" title=" Send Now ">
</div>
did i miss something? i really need help.
Try setting this property
Webbrowser.AllowNavigation = True

Save password prompt disappears when changing location

I'm running a login form, which - in case of success - forwards the user to a specific page via JS location.href. The browsers correctly recognize the form as a login form and offer to save the password. Now only in Google Chrome, the prompt disappears once the location changes. So the prompt is visible for just a split second, making it impossible to save the password.Is there any solution for this? Refreshing after login success is a common thing, so there should be a way to fix this..
Edit:
This is what the form looks like:
<form id="loginform" action="process.php" target="processframe" method="POST">
<input id="login_name" name="name" type="text" placeholder="Username"><br>
<input id="login_password" name="password" type="password" placeholder="Password"><br>
<button>Submit</button>
</form>
<iframe src="" id="processframe" name="processframe" style="display:none;"></iframe>
So the request is processed in an iframe. process.php then calls a javascript function:
window.setTimeout("parent.loginsuccess()", 1000);
The loginsuccess() function:
function loginsuccess()
{
location.href="/home.php";
}

DOJO 1.8 /dojo/request/iframe is deleting <form></form> when uploading a file

I'm seeing a really strange behavior under DOJO 1.8.0. I'm trying to asynchronously upload a file. The file is uploading just fine and I'm getting the payload as expected, but when clicking the submit button the tags and everything in between is inexplicably deleted! It just vanishes. (NOTE: I've isolated this testing to a test page, so there's nothing else at play that would account for it. You're looking at the entirety of the code.)
require(['dojox/form/Uploader',
"dojo/request/iframe",'dojo/dom','dojo/on',
'dojox/form/uploader/plugins/IFrame', 'dojo/domReady!'],
function(Uploader,iframe,dom,on){
on(dom.byId("myButton"), "click", function(){
iframe.post("UploadFile.php",{
form: dom.byId("myForm"),
handleAs: "json"
}).then(function(data){
console.log(data);
}, function(err){}
);
});
<form method="post" id="myForm" enctype="multipart/form-data" >
<input name="uploadedfile" type="file" data-dojo-type="dojox.form.Uploader"
label="Select Some Files" id="uploader" />
<input id="myButton" type="button" value="Submit" />
</form>
Any ideas from anyone with DOJO 1.8 experience? I've been using /dojo/io/iframe just fine with versions 1.6 thru 1.7. This started happening only with 1.8 using the new /dojo/request/iframe code.
See http://jsfiddle.net/seeds/XD4Dc/1/
The form element is set to have target of dojo's injected iframe. Then form gets like, 'dijitHidden' with abs position -1000 top/left. Cant see why tbh.
There's a fix in the fiddle, add to your callback:
with(dom.byId("myForm").style) {
position = "";
left = "";
top = "";
}
I took this issue to the DOJO-Interest group and it was found to be bug with DOJO 1.8.0. It'll be fixed with the upcoming 1.8.1 release.
http://bugs.dojotoolkit.org/ticket/15939
From the bug report:
The form wasn't getting "eaten", but rather the position was getting set on it and moved out of the viewport because of some faulty logic to check if the form was in the DOM. This has been fixed and should be in 1.8.1.