Azure / azure-functions-dotnet-worker

Azure Functions out-of-process .NET language worker
MIT License
428 stars 182 forks source link

No way to access the MethodInfo of function in Middleware #903

Open CEbbinghaus opened 2 years ago

CEbbinghaus commented 2 years ago

When writing Middleware one of the most fundamental thing that almost every middleware will want to do is check the function for its attributes so that the logic can be conditionally applied (e.g an Authorization attribute to mark a function as requiring Authorization).

After digging through the FunctionContext there seem to be no reference to the MethodInfo of the function being called leaving to having to search online for an answer. I thought that surely there was a way to get it without having to reflect into the assembly for the Type and Method but when I came across juunas11/IsolatedFunctionsAuthentication's Implementation and It seemed quite as if it was the only way to access the MethodInfo.

This feels very unstable and hopefully is not intended to stay as is. Are there any plans to supply the MethodInfo and or Type of the containing class so Middleware's will be able to access any Attributes to execute logic conditionally or with additional information?

Really hoping to see this fixed as it make the Middlewares feel like they are only meant for demonstration purposes.

svengrav commented 2 years ago

I guess @kshyju 🦄 was already working on this topic, but it seems to be stale now (branch). I also would like to implement the authorization as part of the middleware, and that's why I followed his progress on that branch. An "official" solution would be great 😊.

There are already many requests for this feature. 😉

fabiocav commented 2 years ago

This is reasonable. We've been expanding the middleware and other APIs based on key scenarios and creating some of the foundation to layer other features.

We'll flag this as a feature request, and while I don't have an ETA I can provide at the moment, this is something we intend to expose in the future.

Jarrich commented 1 year ago

This would still be VERY useful to have.

ErikAndreas commented 1 year ago

any progress here? auth, logging, a lot of things would benefit from this (attribs + middleware), sad to see az func apps always lagging behind 3 year old .net sample doing want we want here https://michaelscodingspot.com/attributes-and-middleware-in-asp-net-core/

Sid-chudasama commented 9 months ago

With FunctionContext you can get attributes as follow. This is in .Net 8 Isolated Functions.

context.GetTargetFunctionMethod().GetCustomAttributes(typeof(AuthorizeAttribute), false).Any()

mdekok commented 9 months ago

With FunctionContext you can get attributes as follow. This is in .Net 8 Isolated Functions.

context.GetTargetFunctionMethod().GetCustomAttributes(typeof(AuthorizeAttribute), false).Any()

@Sid-chudasama: I cannot find this, can you give a reference? Thank you.

bzuidgeest commented 8 months ago

With FunctionContext you can get attributes as follow. This is in .Net 8 Isolated Functions.

context.GetTargetFunctionMethod().GetCustomAttributes(typeof(AuthorizeAttribute), false).Any()

Its not in .net 8, wish it was its from

https://github.com/juunas11/IsolatedFunctionsAuthentication/blob/main/IsolatedFunctionAuth/Middleware/FunctionContextExtensions.cs#L33