My problem is that even when the logged on user is an Admin still he is not able to access the Controller with [Authorize(Roles = "Admin")] attribute.
Note: The codes works perfectly fine on postman testing but not on openid connect.
IdentityServer 4
I have my Identity server 4 set up and working great with OpenId connect. No problem with it.
Client Project
There is a client probject just having a web api which is secured with [Authorize(Roles = "Admin")] attribute.
[ApiController]
[Route("[controller]")]
[Authorize(Roles = "Admin")]
public class WeatherForecastController : ControllerBase
{
......
}
It's URL is https://localhost:5002/WeatherForecast.
Now: I open this secured URL in the browser, I am redirected to the IdnentityServer login page where I enter the username and password of admin user.
After that what happens is that I am redirected to https://localhost:5002/Account/AccessDenied?ReturnUrl=%2FWeatherForecast page (which should not be as user has admin role).
On the console I get the error -
Authorization failed. These requirements were not met: RolesAuthorizationRequirement:User.IsInRole must be true for one of the following roles: (Admin)
On decoding the token on jwt site, I can clearly see "role": "Admin"there. See the below image:
So I can say My token has Admin role claim on it and it should therefore be able to access the secured controller. But it does not happen??
If i remove the roles from attribute attribute - [Authorize] then I am able to access the controller with the token. See image below:
The Codes:
The startup.cs ConfigureServices method is the place where I have added the Openid connect codes:
services.AddAuthentication(options =>
{
options.DefaultScheme = "Cookies";
options.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookies")
.AddJwtBearer(options =>
{
options.Authority = "https://localhost:5001";
options.Audience = "IS4API";
})
.AddOpenIdConnect("oidc", options =>
{
options.Authority = "https://localhost:5001";
options.ClientId = "postman";
options.ResponseType = "code";
options.Scope.Add("fullaccess");
options.SaveTokens = true;
});
I am not sharing the codes for IdentityServer because the token is generating property and I don't think there is any problem on IdentityServer.
So what can be the problem here, why roles based IdentityServer 4 authentication failing on my case?



