This is using ASP.NET 2.0 in an IIS 6 world.
I have a user submitting a form that sends the data to be via POST. The page receiving the data does some simple validations. If validation passes, then a black-box code routine runs that basically reads the data using Request.Form("NameHere").
I'd like to be able to change the value of the POST item and then put it back on the POST. I don't have the ability to modify the code that reads the Request.Form("NameHere"), so my work around idea is to modify the data during the page's load event. If I change the value of the POST item, then the black-box code doesn't have to be modified.
Is it possible to change the value of an item on the HTTP POST?
Has anyone done this?
Thanks!
Use GET if you want to read data without changing state, and use POST if you want to update state on the server. Save this answer.
It's safe to bookmark and refresh GETs, so there's no warning from browsers, etc., etc. That said, there is no security difference - so the username password example you give isn't exactly accurate. POST is as easily tampered with or viewed as GET.
Even though it is a bit hacky, there is a way to change the value of a POST variable.
We can use Reflection to mark the Request.Form
collection as non-readonly, change the value to what we want and mark it back as readonly again (so that other people cannot change values). Use the following function:
protected void SetFormValue(string key, string value)
{
var collection = HttpContext.Current.Request.Form;
// Get the "IsReadOnly" protected instance property.
var propInfo = collection.GetType().GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);
// Mark the collection as NOT "IsReadOnly"
propInfo.SetValue(collection, false, new object[] { });
// Change the value of the key.
collection[key] = value;
// Mark the collection back as "IsReadOnly"
propInfo.SetValue(collection, true, new object[] { });
}
I have tested the code on my machine and it works fine. However, I cannot give any performance or portability guarantees.
The current POST cannot be changed, however, you could create a new POST request and redirect to that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With