Here's a .NET Core/5 solution that worked for me without using the Identity Server package.
Basically you build a new dictionary of type <string, StringValues>
out of the existing form collection, modify the values in the dictionary how you want, then create a new FormCollection
from that dictionary and set it to context.Request.Form
. The important thing to remember is that the value which is of type StringValues is just an array of strings!
This example demonstrates me removing a "client_id" field from the request form.
var formDictionary = new Dictionary<string, StringValues>();
var form = context.Request.Form;
foreach (var key in form.Keys)
{
// Only add if key is NOT client_id
if (key != "client_id")
{
form.TryGetValue(key, out StringValues formValues);
formDictionary.Add(key, formValues);
}
}
FormCollection formCollection = new FormCollection(formDictionary);
context.Request.Form = formCollection;
Here is another example of me changing the "client_id" field to "NewValue"
var formDictionary = new Dictionary<string, StringValues>();
var form = context.Request.Form;
foreach (var key in form.Keys)
{
form.TryGetValue(key, out StringValues formValues);
// Change client_id value to "NewValue"
if (key == "client_id")
{
formValues = new string[] { "NewValue" };
}
formDictionary.Add(key, formValues);
}
FormCollection formCollection = new FormCollection(formDictionary);
context.Request.Form = formCollection;