1

Topic: AspNetCore. How in Middleware to learn presence of the filter from a caused method?

Greetings.
It is necessary for me in Middleware to check presence IAllowAnonimous of the filter at caused . How it can be made?
Tried through

_httpContextAccessor.HttpContext.Features.Get< IAllowAnonimous> ()

But there it did not appear for some reason. Also thought to quit on it through

_httpContextAccessor.HttpContext.Features.Get< IHttpAuthenticationFeature> ()

With subsequent searches in this list, but also did not find.
How it can be solved?

2

Re: AspNetCore. How in Middleware to learn presence of the filter from a caused method?

WaspNewCore;
In middleware attributes of actions and controlers are inaccessible, as they operate in middleware, implementing MVC, accordingly neither to, nor after - they are not present, as in middleware are not present either controlers, or actions, it only abstraction of a pattern.
All operations which you want to implement in operation MVC pipeline become filters , instead of middleware.
If you tell, that you finally want, can be we find the decision.

3

Re: AspNetCore. How in Middleware to learn presence of the filter from a caused method?

Thanks that responded.
I want to make the following:
In Header it is set certain   - string ID. It is read out separate Middlewar' and registers in scoped service as a constant. This service then  in a repository, also serves as a certain global filter in a condition where. It is not anchored to the user. The user selects this from the operation beginning scoup and in it works. Thus, if it  in  is not present, the error 401 falls out. This certain similarity of a role or certain code Area in which the user in the given session works. It is impossible to alter a danyj functional - it does not turn out to transfer it somewhere to a profile of the user or still something. I.e. the initial data is that, and further I can already work.
I want to make   [NoId] which should be traced Middleware' and at its presence he should not try to consider it from Header . Even if it is (casually came or is not casual). Accordingly should not try to push it in scoped service which will use further.
A certain analog  AllowAnoimous turns out.

4

Re: AspNetCore. How in Middleware to learn presence of the filter from a caused method?

Basically, can make it through the filter?
That the filter read out  and wrote down it where it is necessary. And still to make NotFilter (its antipode) which would ignore this .
Thus to configure, that the main  was applied by default to all methods.  its antipode will be  presence of the first.
Here only as it to make, that the filter an antipode disconnected core operation? Probably in the core it is possible, , to get access to applied filters (what I and wanted to make initially) and is simple to do nothing at presence the antipode filter? I.e. the filter an antipode appears in a role of the marker interface.
So?

5

Re: AspNetCore. How in Middleware to learn presence of the filter from a caused method?

WaspNewCore;
You need to make the filter of authorization which authorizes or not the user looking at request title. But consider that the user can palm off any  what wishes, therefore safety  should become at the moment of authorization, register in , instead of analog  the user, it is a hogwash.

6

Re: AspNetCore. How in Middleware to learn presence of the filter from a caused method?

WaspNewCore wrote:

This service then  in a repository, also serves as a certain global filter in a condition where.

  self-peep?

7

Re: AspNetCore. How in Middleware to learn presence of the filter from a caused method?

hVostt wrote:

the filter of authorization which authorizes or not the user looking at request title

And to throw it on actions or controlers.

8

Re: AspNetCore. How in Middleware to learn presence of the filter from a caused method?

hVostt wrote:

WaspNewCore;
You need to make the filter of authorization which authorizes or not the user looking at request title. But consider that the user can palm off any  what wishes, therefore safety  should become at the moment of authorization, register in , instead of analog  the user, it is a hogwash.

There there is already good authorization with . Given  in  the wish from managers of the project of a manual) is pure ,
Petro123;
Yes unpretentious . Purely for injection.

9

Re: AspNetCore. How in Middleware to learn presence of the filter from a caused method?

WaspNewCore wrote:

Thanks that responded.
I want to make the following:
In Header it is set certain   - string ID. It is read out separate Middlewar' and registers in scoped service as a constant. This service then  in a repository, also serves as a certain global filter in a condition where. It is not anchored to the user. The user selects this from the operation beginning scoup and in it works. Thus, if it  in  is not present, the error 401 falls out. This certain similarity of a role or certain code Area in which the user in the given session works. It is impossible to alter a danyj functional - it does not turn out to transfer it somewhere to a profile of the user or still something. I.e. the initial data is that, and further I can already work.
I want to make   [NoId] which should be traced Middleware' and at its presence he should not try to consider it from Header . Even if it is (casually came or is not casual). Accordingly should not try to push it in scoped service which will use further.
A certain analog  AllowAnoimous turns out.

Did not understand at what here  AllowAnoimous.
"In Header it is set certain   - string ID. It is read out separate Middlewar' and registers in scoped service as a constant." - that is operation is conducted in a context there is nobody ID that is written to service.
and  this service to properties IsAnonymous also use.

10

Re: AspNetCore. How in Middleware to learn presence of the filter from a caused method?

And by the way it is not clear what for here generally middleware?
In the designer of this your service  IHttpContextAccessor and let he gets necessary to it  attributes.

11

Re: AspNetCore. How in Middleware to learn presence of the filter from a caused method?

skyANA wrote:

In the designer of this your service  IHttpContextAccessor and let he gets necessary to it  attributes.

It there does not get them. Controlers, actions, areas is scope MVC, aspects are accessible only in filters MVC, but in any way through HttpContext. A maximum, it is possible  in RouteData and to derive there names of controlers. It is possible certainly  types and most to derive attributes, but it is full , it is impossible so to do.

12

Re: AspNetCore. How in Middleware to learn presence of the filter from a caused method?

WaspNewCore wrote:

There there is already good authorization with . Given  in  the wish from managers of the project of a manual)

is pure ,
In general, filters do, authorizations, or on actions, there do that want.

13

Re: AspNetCore. How in Middleware to learn presence of the filter from a caused method?

That type:

public interface ISecurityContext
{
bool IsAnonymous {get;}
string UserRole {get;}
}
using Microsoft. AspNetCore. Http;
using System. Linq;
public class SecurityContext: ISecurityContext
{
private readonly bool anonymous;
private readonly string userRole;
public bool IsAnonymous => anonymous;
public string UserRole => userRole;
public SecurityContext (IHttpContextAccessor context)
{
var headers = context. HttpContext. Request. Headers;
headers. TryGetValue ("X-IsAnonymous", out var anonymousValues);
int. TryParse (anonymousValues. FirstOrDefault (), out anonymous);
headers. TryGetValue ("X-UserRole", out var userRoleValues);
int. TryParse (userRoleValues. FirstOrDefault (), out userRole);
}
}
class SomeRepository: ISomeRepository
{
private readonly ISecurityContext securityContext;
public SomeRepository (ISecurityContext securityContext)
{
this.securityContext = securityContext;
}
}
//This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices (IServiceCollection services)
{
//Add framework services.
services. AddHttpContextAccessor ();
//Register application services.
services. AddScoped <ISecurityContext, SecurityContext> ();
services. AddScoped <ISomeRepository, SomeRepository> ();
}

14

Re: AspNetCore. How in Middleware to learn presence of the filter from a caused method?

hVostt wrote:

it is passed...
In general, filters do, authorizations, or on actions, there do that want.

And here it?
The pipeline or how correctly term?
https://docs.microsoft.com/ru-ru/aspnet … pnetcore2x

15

Re: AspNetCore. How in Middleware to learn presence of the filter from a caused method?

hVostt wrote:

it is passed...
It there does not get them...

Why does not get?
I so understood that is told certain  title in request that defines a role of the user.
Also it is necessary to select the data from a context of the given role.
Example above.

16

Re: AspNetCore. How in Middleware to learn presence of the filter from a caused method?

skyANA wrote:

Why does not get?
I so understood that is told certain  title in request that defines a role of the user.
Also it is necessary to select the data from a context of the given role.
Example above.

You seem generally understood nothing smile))
It is a question not of how to get title, and how to check up, whether there is at current action or the controler an attribute [AllowAnonymous].
IN middleware.
And you offered any wrapper to get values . To tell the truth, here would approach steam of methods of the extension for HttpContext, what for here it here to fence?

17

Re: AspNetCore. How in Middleware to learn presence of the filter from a caused method?

skyANA;

WaspNewCore wrote:

I want to make   [NoId] which should be traced Middleware' and at its presence he should not try to consider it from Header . Even if it is (casually came or is not casual). Accordingly should not try to push it in scoped service which will use further.

I.e. he already solved the task to push the ID from  in .
And now how to make, that on some routes it to ignore by means of attribute.
The route is understood as controler action as only there it is possible to apply attribute, as aspect of action.

18

Re: AspNetCore. How in Middleware to learn presence of the filter from a caused method?

hVostt;
We read the selected:

WaspNewCore wrote:

Thanks that responded.
I want to make the following:
In Header it is set certain   - string ID . It is read out separate Middlewar' and registers in scoped service as a constant . This service then  in a repository, also serves as a certain global filter in a condition where. It is not anchored to the user. The user selects this from the operation beginning scoup and in it works. Thus, if it  in  is not present, the error 401 falls out. This certain similarity of a role or certain code Area in which the user in the given session works. It is impossible to alter a danyj functional - it does not turn out to transfer it somewhere to a profile of the user or still something. I.e. the initial data is that, and further I can already work.
I want to make   [NoId] which should be traced Middleware' and at its presence he should not try to consider it from Header . Even if it is (casually came or is not casual). Accordingly should not try to push it in scoped service which will use further.
A certain analog  AllowAnoimous turns out.

Explain, what such Header? What is  attribute - string ID? Time you up to the end understood the author:-Q

19

Re: AspNetCore. How in Middleware to learn presence of the filter from a caused method?

He to a campaign the same terms names different things:-Q
But not an essence.
My idea that if now causing side defines a context, transferring titles let it and continues it to do.
That is type if I pull this url from under the administrator service gives me the data without superfluous filters.

20

Re: AspNetCore. How in Middleware to learn presence of the filter from a caused method?

skyANA wrote:

Explain, what such Header? What is  attribute - string ID? Time you up to the end understood the author:-Q

Yes like clear.
He wants [NoId] to ignore in the presence of attribute any hogwash from Header.
To attribute from middleware it cannot receive access, it needless to say as it needs to be implemented in the form of the filter of infrastructure MVC.
And that such "  - string ID" it is any hogwash which is set in .
I.e. when I asked to sound the finite task, the author sounded instead implementation giblets as it it understands.

skyANA wrote:

My idea that if now causing side defines a context, transferring titles let it and continues it to do.

Emmas. Titles? Or type &ignoreAuthFilter=true? smile))

skyANA wrote:

That is type if I pull this url from under the administrator service gives me the data without superfluous filters.

Generally, it is an authorization question, instead of any titles.

21

Re: AspNetCore. How in Middleware to learn presence of the filter from a caused method?

hVostt;
I so understood that in title the parameter that defines from under what role is told service twitches
If did not transfer, 401 Unauthorized
On idea it is necessary to get a certain super-puper role, which given to give without restrictions
And he for some reason wants to leave also the current mechanism, and sideways a crutch to fasten:-Q

22

Re: AspNetCore. How in Middleware to learn presence of the filter from a caused method?

hVostt wrote:

Generally, it is an authorization question, instead of any titles.

Listen if to push in Authorization title JWT Token at you questions I think does not arise.
And here they made the :-Q

23

Re: AspNetCore. How in Middleware to learn presence of the filter from a caused method?

hVostt me understood truly. Thanks to an explanation that  applied to controlers are processed MVC  and have no sense for others , in a head all rose on the places.
I rewrite on IAsyncResourceFilter, like it turns out. Anything difficult. Simply not there dug initially. It is possible to close a subject smile Thanks.

24

Re: AspNetCore. How in Middleware to learn presence of the filter from a caused method?

skyANA wrote:

I so understood that in title the parameter that defines from under what role is told service twitches
If did not transfer, 401 Unauthorized

To tell the truth, even with your explanation, I not absolutely understand the finite task smile

skyANA wrote:

on idea it is necessary to get a certain super-puper role, which given to give without restrictions
And he for some reason wants to leave also the current mechanism, and sideways a crutch to fasten:-Q

Or to enter with defined Claim on which it is possible to build policies and  attributes.

25

Re: AspNetCore. How in Middleware to learn presence of the filter from a caused method?

skyANA wrote:

Listen if to push in Authorization title JWT Token at you questions I think does not arise.

Is not present certainly, there it is just necessary to check up the signature smile