Getting a Method Not Allowed Error When Using ASP.NET Web API Inside ASP.NET Web Forms

I am trying to implement HMAC authentication using the code given here: http://bitoftech.net/2014/12/15/secure-asp-net-web-api-using-api-key-authentication-hmac-authentication/ .

I have included this code in my ASP.NET web form application. I created a folder called "HMACAPI" and added controllers and filters to it. I also installed all the necessary Nuget packages. This is how I implement my maintenance methods:

[HMACAuthentication] [RoutePrefix("api/forms")] public class FormsController : ApiController { [Route("")] public IHttpActionResult Get() { ClaimsPrincipal principal = Request.GetRequestContext().Principal as ClaimsPrincipal; var Name = ClaimsPrincipal.Current.Identity.Name; return Ok("test"); } [Route("")] public IHttpActionResult Post(string order) { return Ok(order); } } 

This is my route configuration for the API:

 GlobalConfiguration.Configure(APIWebFormsProject.API.WebApiConfig.Register); 

But when I use client.PostAsJsonAsync() , it shows a Method Not Allowed error. I tried different questions, but none of their answers help.

What I tried:

  • Removed WebDAV module.

  • Added [HttpPost] attribute for post method.

I am using the URL http: // localhost: 56697 / api / forms / to access the API. But I also tried " http: // localhost: 56697 / api / forms " and " http: // localhost: 56697 / api / forms / test ".

UPDATE

As Obsidian Phoenix suggested, I was able to run it without the [HMACAuthentication] attribute. But I want to implement this using HMAC authentication. So what could be the reasons for this?

+7
c # asp.net-web-api webforms
source share
3 answers
Finally, I solved it. I did not expect this to be a solution.

I changed the port and local IIS server instead of Visual Studio Development Server. Although I used IIS before I asked a question, but I think that changing the port solved the problem.

Well, thank you all for your efforts in solving my problem. Due to all your answers and comments, I was able to come to this decision. :)

0
source share

I think your problem is sending HTTP POST to the endpoint (api / forms) and has nothing to do with the HMACAuth attribute, right?

If so, then do not send Order as String, it should be like a POCO object containing a string property, something as shown below:

 public class OrderModel { public string Order { get; set; } } 
+3
source share

Your method [FromBody] not have the [FromBody] attribute.

To use client.PostAsJsonAsync(url, "test") , your method signature should look like this:

 [Route("")] public IHttpActionResult Post([FromBody] string order) { return Ok(order); } 

Similarly, passing the POCO object:

 [Route("")] public IHttpActionResult Post([FromBody] OrderModel order) { return Ok(order); } 
+3
source share

All Articles