When I put the [AllowAnonymous] attribute at the controller level and the [Authorize] attribute at the method level then Authorize is not working if I removed AllowAnonymous from the controller then it's working.
Loading
When I put the [AllowAnonymous] attribute at the controller level and the [Authorize] attribute at the method level then Authorize is not working if I removed AllowAnonymous from the controller then it's working.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Jaimin ShethiyaPosted Mar 28, 2024, 5:10 AM
Hello @Alpesh,
You are doing the reverse case, you need to add [Authorize] attribute at controller level and [AllowAnonymous] attribute add in the endpoints level.
Thanks
Tuhin PaulPosted Mar 14, 2024, 8:05 PM
GetPublicDataallows anonymous access because of[AllowAnonymous].GetPrivateDatarequires authorization because only[Authorize]is present.[AllowAnonymous]sparingly only on specific methods that genuinely need anonymous access.Tuhin PaulPosted Mar 14, 2024, 8:04 PM
Behavior of `[AllowAnonymous] at Controller Level:
[AllowAnonymous]to a controller, it essentially bypasses all authorization checks for all actions within that controller.[Authorize]attribute, will be accessible to unauthenticated users.Conflicting Attributes:
[AllowAnonymous]on the controller takes precedence over[Authorize]on individual methods.Solution:
[AllowAnonymous]only on specific methods within the controller that should be accessible without authentication.Jignesh KumarPosted Mar 13, 2024, 5:41 AM
Hello Alpesh,
You have done reverse, you should add
[Authorize] at controller level and
[AllowAnonymous] at action method level. If you do this change then it should work for you.
Jaimin ShethiyaPosted Mar 13, 2024, 4:24 AM
Hello Alpesh,
Yes that is now working for Authorize because you have added the AllowAnonymous attribute.
If you want both then it is not possible.
If you want to each an every endopint via executable with the Authorize attribute then you need to implement Authorization based on either Role or Policy based authorization.
Naimish MakwanaPosted Oct 6, 2023, 10:06 AM
In ASP.NET, when you use the
[AllowAnonymous]attribute at the controller level, you are essentially telling ASP.NET that all the actions (methods) within that controller are exempt from authorization checks. This means that even if you use the[Authorize]attribute on individual methods within the controller, those methods will still be accessible without authentication because the controller-level[AllowAnonymous]attribute takes precedence.If you want to have some actions within the controller that are authorized while others are not, you should remove the
[AllowAnonymous]attribute from the controller level and apply the[Authorize]attribute only to the specific methods (actions) that need authorization. Here's an example of how you can do that:Thanks
Naimish Makwana