Bearer token is not used by Swagger for endpoints with [authorize], but used for [AllowAnonymous]
Hey!
Yesterday while working on a project for university I encountered a strange bug, which can be seen in the screens below:
- When an endpoint is marked with the
[Authorize]
attribute, even if a Bearer Token is specified in Swagger, this Token will not be used in the execution of the request.
- If we use the [AllowAnonymous]
attribute instead of [Authorize]
, the Bearer Token will be sent in the request.
The described behaviour can be seen in the screenshots. Does anyone know what might be generating it? And how can I fix it?![No description](https://answer-overflow-discord-attachments.s3.us-east-1.amazonaws.com/1248950780594094140/image.png)
![No description](https://answer-overflow-discord-attachments.s3.us-east-1.amazonaws.com/1248950781051277322/image.png)
![No description](https://answer-overflow-discord-attachments.s3.us-east-1.amazonaws.com/1248950781403594853/image.png)
![No description](https://answer-overflow-discord-attachments.s3.us-east-1.amazonaws.com/1248950781785280522/image.png)
![No description](https://answer-overflow-discord-attachments.s3.us-east-1.amazonaws.com/1248950782187929640/image.png)
4 Replies
my Program.cs
endpoint in UserController.cs
Nevermind, I found the reason why it didn't work.
For some reason, adding
opts.OperationFilter<SecurityRequirementsOperationFilter>();
to the Swagger config disabled attaching bearer token to the request, when it had [Authorize]
attribute on itself.![No description](https://answer-overflow-discord-attachments.s3.us-east-1.amazonaws.com/1248954200247894036/image.png)
Unknown User•5w ago
Message Not Public
Sign In & Join Server To View