0

I am creating an ASP.NET Core 6.0 Web API.

This is my controller

public virtual IActionResult GetStatus([FromHeader][Required()] string authorization, [FromRoute][Required] string requestId)
{ }

Problem is the request header is not accepting a parameter with name authorization. If I change name to something else it works fine. But when I change name to authorization, it is not working. I added a middleware and see that the request header does not have authorization in header.

My organization specification wants the name to be authorization only so I can not have any other name here.

Is there any way a Web API endpoint can accept a parameter named "authorization".

I tried creating a custom AuthenticationHandler, it does not solve the problem.

1 Answer 1

0

Is there any way a Web API endpoint can accept a parameter named "authorization"

Reason: 1

Well, your issue is pretty obvious. You may hear about reserved keyword. Like in C# there are some compiler-reserve key for example static, class public and so on. We cannot set variable name as reserve key word.

If you navigate to System.Net.Http.Headers namespace and then move to below details, you would see Authorization is a reserve key word for HttpClientb request and its not wise to manipulate that system reserve word at all.

client.DefaultRequestHeaders.Authorization

enter image description here

Reason: 2

Likewise, if you use or attached Swagger in your program.cs so no longer you cannot use authorization, Content-Type, Accept as your self defined parameter, even if you use it will be skipped. However, Postman can allow you to do that. You can check swagger official reference here.

You can see the example below:

Swagger Request:

enter image description here

Postman Request:

enter image description here

Note:

Apparently, you can ommit swagger then it might work. Nonetheless, if you are using Authorization and Authentication within your application, then you might face unexpected issues if you still stick to use authorization as your custom parameter.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.