I am using isentittyserver4 and I authorized my controller or actions.
[Authorize(Roles ="app.admin")]
[Route("products")]
public class ProductsController : Controller
{
}
My token contains roles. I can access roles in User
object property.
- User.IsInRole("app.admin"); //false
- User.IsInRole("app.viewer"); //true
But if I send request with token that does not contain app.admin
but contains app.viewer
But request response is 403 forbidden. But it shold be 401 unauthorized.
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
.AddIdentityServerAuthentication(options =>
{
options.Authority = Configuration.GetValue<string>("Authority");
options.ApiName = Configuration.GetValue<string>("ApiName");
options.RequireHttpsMetadata = false;
});
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseAuthentication();
app.UseMvc();
}
}