3

I have build custom Identity Middle-ware. Inside Invoke method I am checking if request has token etc. After token is checked I want to pass request further to controller. It`s working for GET request - it jump into controller method. It is not working for POST request.

Here is Invoke Method

public async Task Invoke(HttpContext context)
{
    //checking
    await _next(context);
}

Its working controller:

[Route("api/[controller]")]
public class ValuesController : Controller
{
    // GET api/values
    [AllowAnonymous]
    [HttpGet]
    public string Get()
    {
        return "Allow annymous";
    }
 }

And not working one

[Route("api/[controller]")]
public class AccountController : Controller
{
    [AllowAnonymous]
    [HttpPost]
    public void Login()
    {
         //some logic
          HttpContext.Response.WriteAsync("Unauthorized");
    }
}

Making POST call Postman returns 404 not found.

2
  • You are surely making POST request to /api/Account? Commented Jan 20, 2017 at 16:58
  • Is there any other POST methods in thecsame controller? Commented Jan 20, 2017 at 17:03

3 Answers 3

1

The route for the action in question would be /api/Account. Make sure that is correct.

If instead you wanted /api/Account/Login:

[Route("api/[controller]/[action]")]
public class AccountController : Controller
{
    [AllowAnonymous]
    [HttpPost]
    public void Login()
    {
         //some logic
          HttpContext.Response.WriteAsync("Unauthorized");
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try returning something like an IActionResult rather than simply void.

[AllowAnonymous]
[HttpPost]
public IActionResult Login() 
{
    // some logic
    return Unauthorized();
}

3 Comments

Can you try removing [AllowAnonymous] and see if same issue exists? Also try adding a specific route at the class level like [Route("api/foo")] and hitting the POST against "api/foo" and see if you get response.
How are you executing the POST exactly? What does the URL look like? Are you adding any header values or do you have anything in startup.cs what would prevent POST from happening like authentication restrictions?
I am doing it on default ctonroller values and doing this by postman now without any middleware or anything like that and it return 500 and the point is i can check what is really error
0

Looking at your code I hopping there is only one method inside the AccountController called Login. If not please add attribute [Route("Login")] to Login method (make sure you do not keep it empty like [Route("Login")] otherwise it will have same as what you are doing currently).

Then make the call to the POST http://host:***/api/account/login url and that should work.

FYI: I have tried this thing on my machine and it is working.

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.