Disable HTTP Options method in ASP.Net Core 3.1

4.3k views Asked by At

We have a requirement of disabling the HTTP OPTIONS method in an ASPNET Core Web application due as a part of security fixes. How can we disable the HTTP OPTIONS method in ASP.Net core 3.1 API?

1

There are 1 answers

0
Yiyi You On BEST ANSWER

Here is a demo with middleware: Add this to your startup Configure:

 app.Use(async (context, next) =>
            {
                // Do work that doesn't write to the Response.
                if (context.Request.Method=="OPTIONS")
                {
                    context.Response.StatusCode = 405;
                    return; 
                }

                await next.Invoke();
                // Do logging or other work that doesn't write to the Response.
            });

result: enter image description here

Or you can apply [HttpGet] [HttpPost] [HttpPut] ... on your action method in controller.Here is an official document about the Http Verbs.