I'm trying to implement authentication and access control with IdentityServer4 on an ASP.NET MVC Core app (.NetCore 2). While it's not the first time I implement a backend, it's the first time with .net, and I'm struggling with some things.
I've followed the instructions at https://identityserver4.readthedocs.io/en/release/quickstarts/1_client_credentials.html as well as the page before that.
I have also added the sample IdentityController
as they show:
using System.Linq;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace leafserver.Controllers
{
[Route("/api/identity")]
[Authorize]
public class IdentityController : Controller
{
[HttpGet]
public IActionResult Get()
{
return new JsonResult(from c in User.Claims select new { c.Type, c.Value });
}
}
}
There are a few differences between my implementation and their example. As far as I can see:
- I'm serving on my local network address (192.168.1.x) instead of localhost
- They're using a "Web Application", where I'm using a "Web Api"
- They seem to use
ControllerBase
instead ofController
as a superclass - I'm not sure whether there's a difference between the ASP.NET MVC they use and the one I use (I'm using core, they don't seem to, but normally it should still work...)
What I noticed is the following:
- as long as I don't put a
[Authorize]
, all is well. I get a 200 OK with the expected result - when the
[Authorize]
annotation is there, but I use no authentication bearer token, I am redirected to the login page (which doesn't work since this is a web api, but that's a problem for later) - when the
[Authorize]
annotation is there, and I use (what I think is) a correct authentication token, I get a 404 response.
I was expecting to have a 401 response instead. Why would my routing not work because I'm using an authentication token?
Also, I'm not getting any log from the server, which doesn't help...