I'm trying to implement a basic version of the oauth flow using auth0 services. I've followed the basic setup and fetching a bearer token works but for some reason I can't get access to my controller. This is my startup class:
namespace Webapi
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddControllers();
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{
options.Authority = "https://groep5.eu.auth0.com/";
options.Audience = "https://localhost:44346/home/";
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseAuthorization();
app.UseAuthentication();
app.UseEndpoints(endpoints =>
{
endpoints.MapDefaultControllerRoute();
});
}
}
}
And this is my controller:
namespace Webapi.Controllers
{
public class HomeController : Controller
{
[Authorize]
public string Leden()
{
return "retlok";
}
public string Test()
{
return "random tekst";
}
}
}
My first thought was that I had the permissions wrong, but I've added my method leden in multiple ways. I've done "leden", "https://localhost:44346/home/leden" and "read:leden". My problem is probably something simple, but I've been at it for a bit too long, so I thought it was easier to ask it on this website.
UseAuthenticationandUseAuthorizationcalls switched around the wrong way. Maybe it was just that, but glad you got it going anyway. The order in which you inject middleware is important in many cases, and MS have a page documenting their recommended order: learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/…