-
Notifications
You must be signed in to change notification settings - Fork 186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Accessing HttpRequestData in middleware #414
Comments
This isn't currently possible. See #340 |
While waiting for a proper solution, this works: public static class FunctionContextExtensions
{
public static HttpRequestData GetHttpRequestData(this FunctionContext functionContext)
{
try
{
KeyValuePair<Type, object> keyValuePair = functionContext.Features.SingleOrDefault(f => f.Key.Name == "IFunctionBindingsFeature");
object functionBindingsFeature = keyValuePair.Value;
Type type = functionBindingsFeature.GetType();
var inputData = type.GetProperties().Single(p => p.Name == "InputData").GetValue(functionBindingsFeature) as IReadOnlyDictionary<string, object>;
return inputData?.Values.SingleOrDefault(o => o is HttpRequestData) as HttpRequestData;
}
catch
{
return null;
}
}
} And you can use it like this: public class CustomMiddleware : IFunctionsWorkerMiddleware
{
public async Task Invoke(FunctionContext context, FunctionExecutionDelegate next)
{
HttpRequestData httpRequestData = context.GetHttpRequestData();
// do something with httpRequestData
await next(context);
}
} |
Thank you for opening the issue and the workaround shared. Currently, as you've found, the context carries the raw information and the services that would enable binding processing are not publicly exposed. This is work that is planned and, although I don't have a concrete ETA, we hope to expose soon. Keeping this open so we can update with progress. |
Any change of a work around for the missing OutputData, something we can reflect to retrieve the HttpResponseData class? I inspected the data at runtime and cannot find anything similar |
Yes I am also struggling with this. I need to set some http headers for all requests. Even if I create a service that I inject into each function it's still really messy. |
The workaround is so hacky it almost shouldn't be used, but here goes:
I hope the proper interfaces get exposed publicly soon. |
Hi, How can we get access to HttpContext object to set User Claims? In my case I'm using custom Authorization lib to validate JWT token. Upon successful validation I need to set user claims to HttpContext so that I can use that in other parts of function app method execution. Thank you! |
Any updates on this about official support? |
Wondering about when we will see official support for this as well 👍 For anyone interested, I have this small utility class that helps with both the request and response: https://github.com/elmahio/Elmah.Io.Functions.Isolated/blob/main/src/Elmah.Io.Functions.Isolated/FunctionContextExtensions.cs. All credits goes to @david-peden-q2 and @JanosNollFD who provided the code for this class through comments in this thread. I only aligned the two messages and added a null check if I remember correctly. |
@ThomasArdal I tried |
@arashcomsense Don't know. I think you should use StackOverflow or create another issue since that is out of the scope of this issue. |
As someone that has gone through trying to figure out how to do authentication using middleware for this SDK, this is not the experience I'd like developers to go through. Isolated Worker SDK middleware is a solid tool to tap into the pipeline. It falls short by supporting anything other than the trivial termination or enrichment. Combined with the forced reflection to access specific types needed at runtime, it casts a massive shadow on the maturity of this SDK despite the GA label. More samples would help but would likely add the burden on the team to maintain those. Not to mention the hesitation to show how to do things in a "wrong" way. Support for SDK types, imperative bindings, Application Insights, access to Functions SDK types, all these issues point to the same root problem - missing clarity on the path going forward for this SDK. Not to mention the convergence of the two SDKs into one after .NET 7. |
@SeanFeldman Very well said. |
You can use the
Please refer the Thanks for your patience. Please let us know if you have questions. |
As per the sample I am able to build a custom middleware which accesses the
FunctionContext
and works with it. However, how do I access theHttpRequestData
object?Some broader context: I am trying to build an authentication middleware: https://hajekj.net/2022/04/22/azure-functions-out-of-process-and-authentication-with-azure-ad/
I wanted to do it via
context.Features.GetRequired<IFunctionBindingsFeature>();
butIFunctionBindingsFeature
is internal. Another approach would be to go via the raw data:context.BindingContext.BindingData;
but that would require me to manually parse the headers JSON.So far, I managed to parse out the headers and seems to work fine - but it looks really dirty to me - https://github.com/hajekj/azure-functions-dotnet-worker-miw/blob/master/WorkerAuthentication/AuthenticationMiddleware.cs.
Also, I would like to be able to modify the response - for example to return HTTP 401 when token is not present, but I didn't manage to figure out how to do it in middleware. Is there any way to do it - or modify the Function output in general?
The text was updated successfully, but these errors were encountered: